Why is all of the following necessary:

invoke SelectObject,mDC,hBmp
mov hOld,eax

invoke SelectObject,mDC,hOld
invoke DeleteDC,mDC

Why can't I just use the first and last lines of code in my paint procedure since I'm going to delete the DC anyway?

Couldn't I just write:

invoke SelectObject,mDC,hBmp
invoke DeleteDC,mDC

?
Posted on 2001-11-28 23:10:50 by grv575
I will take a stab at this, but im *not* the authority for GDI workings:

invoke SelectObject,mDC,hBmp
mov hOld,eax


Because even though we dont care what was ever on the DC before we load the hBmp to it, the MSDN states "This function returns the previously selected object of the specified type. An application should always replace a new object with the original, default object after it has finished drawing with the new object. " So we save it in efforts to keep M$'s fragile memory management system from going belly-up :)

invoke SelectObject,mDC,hOld
invoke DeleteDC,mDC

Thing to remember here is, now that you have restored the origional "object" to the DC, the hBmp was pushed back out in EAX, and unless otherwise taken care of, it too needs to eventually be destroyed!

NaN
Posted on 2001-11-28 23:33:31 by NaN
grv575,

Nan is right, when you work with GDI objects, there is memory being allocated which must be deallocated after otherwise the memory is not recoverable later and the operating system may end up without enough memory to run properly.

The "SelectObject()" API is the one you must be careful with, as NaN said, select the object you want and preserve the handle of the old object, use what you have selected, reselect the old object and then delete the one you created.

These are a set of high level functions that handle the allocation and deallocation of memory, if you don't handle them corectly, you will end up with memory leaks.

Regards,

hutch@movsd.com
Posted on 2001-11-29 03:54:51 by hutch--
Thanks for the explanations. That really clears up what seems to be a useless step when using the gdi functions. I thought it might just be in case you want to use the previously selected object with the dc again, but I guess MS has more lower-level code that requieres this extra code.
Posted on 2001-11-29 12:08:39 by grv575