Archived

This topic is now archived and is closed to further replies.

Zyroth

Win32 prog memory leak?

Recommended Posts

Hi, I''ve been trying to find the error in my program for the last two an a half hours. After drawing the bitmaps some number of times it just won''t draw them anymore and all I get is the background brush color. It sounds like it might be a memory leak, but I''m new to windows programming and can''t seem to figure it out. Here''s the code:
  
int ShowBitmapResource(HDC hDestDC, int xDest, int yDest, int nResID)
{
	HDC hSrcDC;       // source DC - memory device context

	HBITMAP hbitmap;  // handle to the bitmap resource

	BITMAP bmp;       // structure for bitmap info

	int nHeight, nWidth;  // bitmap dimensions


	// first load the bitmap resource

	if ((hbitmap = (HBITMAP)LoadImage(appInstance, MAKEINTRESOURCE(nResID),
                                      IMAGE_BITMAP, 0, 0,
                                      LR_CREATEDIBSECTION)) == NULL)
		return(FALSE);

	// create a DC for the bitmap to use

	if ((hSrcDC = CreateCompatibleDC(NULL)) == NULL)
		return(FALSE);

	// select the bitmap into the DC

	if (SelectObject(hSrcDC, hbitmap) == NULL)
		return(FALSE);

	// get image dimensions

	if (GetObject(hbitmap, sizeof(BITMAP), &bmp) == 0)
		return(FALSE);

	nWidth = bmp.bmWidth;
	nHeight = bmp.bmHeight;

	// copy image from one DC to the other

	if (BitBlt(hDestDC, xDest, yDest, nWidth, nHeight, hSrcDC, 0, 0,
	  	       SRCCOPY) == NULL)
		return(FALSE);

	// kill the memory DC

	
	DeleteDC(hSrcDC);
	

	// return success!

	return(TRUE);
}
  
Now the code prety much comes directly from the Game Programming Genesis Articles. If someone could PLEASE take a quick look thru the code, I would greatly appriciate it. I''m sure that its some small stupid mistake.

Share this post


Link to post
Share on other sites
Ok, I added in the "DeleteObject(hbitmap)" command
(I thought that it would be something stupid like this)
but I ran it afterwards and it still stops displaying the bitmaps after a while... so I''m still at square one,
but thanks, that will probably save me debug time later on .

Share this post


Link to post
Share on other sites
quote:
Original post by Invader X
You never deleted hbitmap


So why there exists a function called DeleteObject() ? You don't delete the hbitmap if you're still using it, but in this case, hbitmap is no longer used after BitBlt is called. The function creates a new handle with LoadImage call everytime the function is called, so the old one is leaking...


Maybe you can keep track where it fails, LoadImage? CreateCompatibleDC? or maybe somewhere else within the function. So, instead of:

if ((hSrcDC = CreateCompatibleDC(NULL)) == NULL) return(FALSE);

add this:
if ((hSrcDC = CreateCompatibleDC(NULL)) == NULL)
{
OutputDebugString(TEXT("Fails in CreateCompatibleDC"));
return(FALSE);
}

and see in which part of that function fails. If none, so it's in another part of your code.


My compiler generates one error message: "Doesn't compile."
-Albert Tedja-


[edited by - nicho_tedja on July 15, 2002 8:48:18 PM]

Share this post


Link to post
Share on other sites
It ended up dying when it tried to load the bitmap:

if ((hbitmap = (HBITMAP)LoadImage(appInstance, MAKEINTRESOURCE(nResID),
IMAGE_BITMAP, 0, 0,
LR_CREATEDIBSECTION)) == NULL)
{
MessageBox(NULL, "1","1", MB_OK | MB_ICONSTOP);
return(FALSE);
}

which would explain why it won''t display them... but I can''t figure out why it''s failing.

Share this post


Link to post
Share on other sites
quote:
From MSDN: SelectObject()
Remark:
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.


    
//...

HGDIOBJ oldObj;
//...

oldObj = ::SelectObject(hSrcDC, hbitmap);
if (oldObj == NULL)
{
//...

}
//...

// ...bitblt here...

::SelectObject(hSrcDC, oldObj);
::DeleteObject(hBitmap);
::DeleteDC(hSrcDC);


... also, it is good practice to have all your resources released/freed for every return path of your function... it seems that your function 'forget' about this.

[edited by - DerekSaw on July 15, 2002 9:41:19 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by nicho_tedja
So why there exists a function called DeleteObject() ? You don''t delete the hbitmap if you''re still using it, but in this case, hbitmap is no longer used after BitBlt is called. The function creates a new handle with LoadImage call everytime the function is called, so the old one is leaking...



I understand English is not your first language so I assume you didn''t notice that my verb ''delete'' was in the past tense. My answer was almost the same as yours, you just posted at almost the same time.



I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader''s Realm

Share this post


Link to post
Share on other sites
quote:
Original post by Invader X
I understand English is not your first language so I assume you didn''t notice that my verb ''delete'' was in the past tense. My answer was almost the same as yours, you just posted at almost the same time.



lol, I thought it was a typo. sorry.

Share this post


Link to post
Share on other sites