Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function BitBlt

Status
Not open for further replies.

ceodav

Programmer
Feb 21, 2003
106
IT
Hi,
i use currently this kind of code to move a bitmap in the device context. The code follows:

CClientDC dc(this);

CDC dcTFT;


dcTFT.CreateCompatibleDC(&dc);

dcTFT.SelectObject(&picture);

CBitmap* pOldBitmap = dcTFT.SelectObject(&picture);
dc.BitBlt(X,Y,bmp.bmWidth, bmp.bmHeight, &dcTFT, 0,0,SRCCOPY);
dcTFT.SelectObject(pOldBitmap);

it works fine but i misunderstand why i should use pOldBitmap.
What would it happen if i try with only dc.BitBlt?

thanks,
Davide


 
You are cleaning up by selecting the bitmap OUT of the device context.

When you select something INTO a device context, you can imagine the device context clamps onto and "owns" the bitmap. Selecting the "old" object back into the device context releases control of the bitmap back to you so you can then do what you like with it.

When you select a bitmap into a device context it allows you to read from or write to the bitmap with drawing functions like BitBlt.
 
This was a big problem in our software. YES, a few times not selecting the old palette, bitmap or whatever seems to cause no problems but over time this can cause big problems. Our software tends to run non stop all week. People start up our app on monday and leave it running all week. We had many memory issues until we cleaned up our code of this problem.

Matt
 
so it's better if i use it

thanks
Davide
 
Yes! It creates what is termed a resource leak which is basically a system level memory leak in the GDI kernel.

-pete
[sub]I just can't seem to get back my IntelliSense[/sub]
 
As a general rule, whatever you select into a device context must eventally be selected out. SelectObject returns the old object of the type you selected in.

See GetObjectType for a list of object types:
Selecting the "old" object back into the device context cleans up.

If you don't clean up properly, you rely on Windows cleaning it up. Windows does not always clean up properly and behavior varies a lot across 9x/NT/XP. At best, it cleans up enough to safely kill the process. DON'T rely on that for long running programs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top