Quantcast
Channel: VBForums - API
Viewing all articles
Browse latest Browse all 168

Questions about cleanups of DCs and Bitmaps

$
0
0
So Lets say I create a DC with CreateCompatibleDC, and then create a Bitmap with one of these 3 functions:
DDB from CreateCompatibleBitmap
DDB from CreateBitmap
DIB from CreateDibSection

Then I select this Bitmap into the DC (using SelectObject) that I had created in the first step, and store the old empty Bitmap (the one that comes by default with a DC whenever a DC is created) in a variable for putting back in that DC in one of the last steps.

Then I do some BitBlts, and other stuff with this in a graphics processing situation.

Then at the end, I need to do some final clean up steps. So the first thing I do now is put the empty Bitmap that originally came with the DC that was created, back into that DC, using SelectObject. Then that frees up the Bitmap (DDB or DIB) that was previously in that DC (and that I had used in my graphics processing). So now I can delete that now freed up Bitmap (no longer associated with a DC) with DeleteObject.

But this is where things get a bit tricky. I'm not sure what to do next. Do I use DeleteDC on my DC, and then DeleteObject on the original empty bitmap that is now back in that DC? Or do I use DeleteObject first on the Bitmap, and then DeleteDC on the DC. Or do I simply do DeleteDC, and then automatically the computer knows to also delete the Bitmap associated with it? Or do I simply do DeleteObject, and then the computer knows to automatically delete the DC that this Bitmap had been selected into?

I've made a number of programs using DCs and Bitmaps, and I'm guessing that if I went back and looked at all of them, I'd find some use one technique for cleanup, and others used another technique. But only one technique I assume is the correct "by the book" technique, and least likely to cause errors, glitches, bugs, crashes, memory leaks, etc. So if somebody can fill me in on this, it would be most helpful.

Viewing all articles
Browse latest Browse all 168

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>