-
Dec 13th, 2014, 12:05 AM
#1
Thread Starter
Frenzied Member
Questions about cleanups of DCs and Bitmaps
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.
-
Dec 13th, 2014, 10:46 AM
#2
Re: Questions about cleanups of DCs and Bitmaps
"By the Book", unselect stuff you created from a DC before destroying it. DeleteObject on an object selected into a DC should fail. This is by the book, but it's not always true as Microsoft will attempt to remember you meant to delete the object, but couldn't because it was in the DC. So when it is later selected out of the DC, then it will get destroyed. This is not documented behavior, it is a matter of trial and error and testing. Should not rely on this behavior.
Besides being restricted to not being able to delete something while it's in a DC, a bitmap can only be selected into one DC at a time, by the book.
You can google & do research on MSDN as much as you want. But the simple rule of thumb:
Originally Posted by msdn
An application should always replace a new object with the original, default object after it has finished drawing with the new object.
- Unselect from the DC, whatever you selected into it, replacing whatever was selected out of it. Do this as soon as you no longer need the object in the DC, especially if you do not control the DC (i.e., via GetDC, GetWindowDC, etc)
- Destroy any DC you created, do not destroy DCs retrieved via GetDC, GetWindowDC, etc
- Destroy any objects you created (rare exceptions do apply)
If you follow the unselect before destruction rule of thumb, can't see any possibility of memory leaks during that process. Also note that SelectObject is not guaranteed to succeed. The object you select into the DC must be compatible with the DC else it fails. Though this is generally not an issue in everyday coding, may want to read the MSDN documentation for SelectObject
Last edited by LaVolpe; Dec 13th, 2014 at 11:03 AM.
-
Dec 16th, 2014, 05:19 PM
#3
Thread Starter
Frenzied Member
Re: Questions about cleanups of DCs and Bitmaps
Originally Posted by LaVolpe
"By the Book", unselect stuff you created from a DC before destroying it. DeleteObject on an object selected into a DC should fail. This is by the book, but it's not always true as Microsoft will attempt to remember you meant to delete the object, but couldn't because it was in the DC. So when it is later selected out of the DC, then it will get destroyed. This is not documented behavior, it is a matter of trial and error and testing. Should not rely on this behavior.
Besides being restricted to not being able to delete something while it's in a DC, a bitmap can only be selected into one DC at a time, by the book.
You can google & do research on MSDN as much as you want. But the simple rule of thumb:
- Unselect from the DC, whatever you selected into it, replacing whatever was selected out of it. Do this as soon as you no longer need the object in the DC, especially if you do not control the DC (i.e., via GetDC, GetWindowDC, etc)
- Destroy any DC you created, do not destroy DCs retrieved via GetDC, GetWindowDC, etc
- Destroy any objects you created (rare exceptions do apply)
If you follow the unselect before destruction rule of thumb, can't see any possibility of memory leaks during that process. Also note that SelectObject is not guaranteed to succeed. The object you select into the DC must be compatible with the DC else it fails. Though this is generally not an issue in everyday coding, may want to read the MSDN documentation for SelectObject
After I replace my own bitmap, with the original default bitmap that came with the DC I created, I then destroy that DC. Does this also destroy the original bitmap? Or do I also need to use a SEPARATE api function to destory the original bitmap?
In other words, assuming this is my starting code.
Code:
dim MyDC as long
dim MyBM as long
dim OldBM as long
MyDC=CreateCompatibleDC(Form1.hDC)
MyBM=CreateCompatibleBitmap(Form1.hDC,640,480)
OldBM=SelectObject(MyDC,MyBM)
For my ending code, should I use this?
Code:
SelectObject MyDC,OldBM
DeleteDC MyDC
DeleteObject MyBM
Or should I use this?
Code:
SelectObject MyDC,OldBM
DeleteDC MyDC
DeleteObject MyBM
DeleteObject OldBM
Note the second use of DeleteObject, in this second piece of code.
-
Dec 16th, 2014, 07:25 PM
#4
Re: Questions about cleanups of DCs and Bitmaps
The original bitmap will be destroyed with the DC -- no worries there, no 2nd DeleteObject call
Tip: DeleteObject SelectObject(MyDC, OldBM). One call vs two
-
Jul 4th, 2015, 02:16 AM
#5
Thread Starter
Frenzied Member
Re: Questions about cleanups of DCs and Bitmaps
Originally Posted by LaVolpe
The original bitmap will be destroyed with the DC -- no worries there, no 2nd DeleteObject call
Tip: DeleteObject SelectObject(MyDC, OldBM). One call vs two
Would this work too (since I will never need the original default bitmap that came with the DC when it was created, and I will have no further use for my bitmap, once I'm done using it in my DC)?
Code:
'initialization code
MyDC = CreateCompatibleDC(some reference hDC)
NewBM = CreateCompatibleBitmap(some reference hDC)
DeleteObject SelectObject(MyDC, NewBM) 'selects my BM into the DC, and deletes the default BM that came with the DC
'cleanup code
DeleteDC MyDC 'destroys the DC and whatever bitmap is in the DC
-
Jul 4th, 2015, 08:41 AM
#6
Re: Questions about cleanups of DCs and Bitmaps
No. You should not be deleting the bitmap that was in the DC when you got the DC. It is not your responsibility & would be a bad habit. Let's say you get a DC from a call like: GetDC, GetWindowDC, etc. That bitmap in the DC is not yours. Also. depending on how the DC was created, the bitmap may be a system stock bitmap which can't be destroyed. Generally speaking, when any DC created by CreateCompatibleDC() is destroyed, the original stock bitmap should be inside of it. What happens to non-stock bitmaps inside the DC when the DC is destroyed? Don't know, never tried to see what happens. Gut feeling is that it is leaked, not destroyed. You can test that if you want -- simply try using the handle to a bitmap, in a BitBlt call, that you think was destroyed.
Also, NewBM should be verified it is not zero. And the SelectObject() call should also be verified it is not zero. SelectObject may fail. See comments for that API that pertains specifically to bitmaps.
Last edited by LaVolpe; Jul 4th, 2015 at 09:07 AM.
-
Jul 4th, 2015, 02:16 PM
#7
Thread Starter
Frenzied Member
Re: Questions about cleanups of DCs and Bitmaps
Never mind, I just realized I made a stupid mistake in my code. The post that was here, I would have deleted, but I can't delete my own posts.
Last edited by Ben321; Jul 4th, 2015 at 02:25 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|