Sign in to follow this  

Must I delete a DIBSection after selecting it into a DC?

This topic is 3491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, currently I am after a memory leak in an application of mine. I use embedded C++, but I think my problem is non specific to the embedded c++ version. A bit of code where I found a problem is the following. It goes through a list of image classes and draws them one after the other the the supplied device context - for the drawing a mask is used.
void DrawImage(HDC hDC, double dWorldOriginX, double dWorldOriginY, uint index) {
	
	if (hDC == NULL) return;
	int		length	= geoTiffs.size();
	HDC		hDCMem	= CreateCompatibleDC(hDC);
	HBITMAP hMemBitmap;
	HBITMAP hbMask;

	// have we got a device context?
	if (hDCMem) {
		// are we in range?
		if (index >= 0 && index < geoTiffs.size()) {
			
			// draw only if layer is uptodate
			if (geoTiffs[index]->isUptodate) {
				
				// create new DIBSection for drawing the layer in
				hMemBitmap = ::CreateDIBSection(hDCMem, geoTiffs[index]->getBitmapInfo(), DIB_RGB_COLORS, (void **)&buffer, NULL, 0);
				if (hMemBitmap) {
					
					// copy the layer data into the DIBSection
					memcpy_s(this->buffer, this->bufferSize, geoTiffs[index]->getBytes(), bufferSize);
					
					// select the DIBSection into temp dc
					SelectObject(hDCMem, hMemBitmap);

					// get the mask of the layer
					geoTiffs[index]->getMask(hbMask);

					// blt the drawn layer through the mask onto our real device context
					MaskBlt(hDC, geoTiffs[index]->getRelativeX(), geoTiffs[index]->getRelativeY(), width, height,
						hDCMem, 0, 0, hbMask, 0, 0,
						MAKEROP4(geoTiffs[index]->getRasterOperation(), 0x00AA0029)); // 0x00AA0029 == Destination

					// Do I need to DeleteObject(hMemBitmap)?
					//DeleteObject(hMemBitmap);
				}
			}
		}
		DeleteDC(hDCMem);
	}
}

Like stated in the source: Do I need to delete the DIBSection after using it or do I not? I thought I have to since memory gets allocated. But if I do so the error code returned by delete object is "6" (=the handle is invalid). So what am I misunderstanding or doing wrong? And if you see any other problems in this code I would be glad to hear about them :) Thanks in advance, Tom

Share this post


Link to post
Share on other sites
Yes, you do. DeleteObject() should do it I think (As you have there). You can usually use Task Manager to check for these sort of leaks if you turn on the "GDI Objects" column.

Share this post


Link to post
Share on other sites
well, thanks.
Like said above I do get an error of DeleteObject (Error Code 6 - invalid handle) if I put it in again.
If I leave it out I will get a memory leak every time I call this funtion - which results in the closing of my app by the OS if the memory is over...

So yes, it seems as I should DeleteObject(...) - but why do I get the error 6?

Thanks,
Tom

Share this post


Link to post
Share on other sites
When you do a SelectObject, you should save the return value and put that back in before deleting whatever you have selected:

HGDIOBJ oldBitmap = SelectObject (hDCMem, hMemBitmap);
...
SelectObject (hDCMem, oldBitmap);
DeleteObject (hMemBitmap);



The same applies to brushes and pens. So if you use a brush too, you'd also need a variable oldBrush.

Of course, after the DeleteObject, the handle is invalid, so you must not use it anywhere else.

Share this post


Link to post
Share on other sites
Thank you very much - I followed your suggestion and saved the old object - and restored it before I deleted the new one.
This alone solved my memory leak problem, too. The MSDN website states that the DeleteObject operation will only succeed if the specified handle is valid and is NOT selected into a device context. And as it seems, I kick the hbitmap out of device context by selecting the old object in. Without the SelectObject the hbitmap was still selected, so the operation could not complete. :)

Thanks again you all,
Tom

[Edited by - VooDooTom on May 28, 2008 2:52:29 AM]

Share this post


Link to post
Share on other sites

This topic is 3491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this