Huge memory LOSS when loading BITMAPS :(

Started by
6 comments, last by FxMazter 21 years, 3 months ago
Hia I have just started out with win32 programming and direct draw... Well, I have written an application that loads a Bitmap with direct draw and does nothing else. The function of loading the bitmap is called every main loop. And after like 20 seconds, the application crashes and windows goes crazy about memory shit... this is how the bitmap loading function looks:
  

int DrawBitmapWithDD(LPDIRECTDRAWSURFACE7 lpdds, int xDest, int yDest, int nResID)
{
	HBITMAP hBitmap;
	HDC hSrcDC;
	HDC hDestDC;
	BITMAP bmp;
	int nWidth, nHeight;

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

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

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

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

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

	if(FAILED(lpdds->GetDC(&hDestDC)))
		return(FALSE);

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

	lpdds->ReleaseDC(hDestDC);
	DeleteDC(hSrcDC);

	return(TRUE);
}

  
Well, what I think might be the problem Is that every time this function is called "LPDIRECTDRAWSURFACE7 lpdds" is created but never released and nullified? That is the parameter where i send the ddsurface where the bitmap is to be putted. I actually don''t know... Maybe someone could help a poor newbie out? THX!
Advertisement
Maybe you have to DeleteObject your hBitmap (or something like that).
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
Is there a reason why you are loading the bmp every single time you go through your main loop? If not, you should only do it during initialization or on the condition that a new load is required.
Yes, you load a bitmap once each frame, and then lose it when hBitmap goes out of scope. It''s still allocated in memory, you just have no reference to it. This repeats over and over until your memory is filled with bitmaps you can''t access.

Read the documentaton for LoadImage.
"When you are finished using the bitmap, cursor, or icon, you can release its associated memory by calling one of the functions in the following table.

Bitmap: DeleteObject
Cursor: DestroyCursor
Icon: DestroyIcon"


The way you should do it is to load the bitmap once before you start your main loop, blit it during your main loop, and then call DeleteObject on it after your main loop. Many game engines are split into Initialize, Frame, and Shutdown functions for exactly this reason.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]
Nah, there is no special reason, except that I will expand this program later on... and it will be an animation with text, so I don''t want the old text stay on the screen. So thats why I will probably need to refresh the screen every main loop. But the thing I mean... Is that it''s not supposed to take this much memmory right? I mean, it should be possible to replace the old screen memmory with the old and so on...

Oh NOW I understand!
Is it like that you only load a bitmap once during initialization... then you recieave a handle to the bitmap memory.

Then every time you want to show that bitmap, you blit it to the ddsurface? And this way there is only one memory allocation?

Am I right with this?



[edited by - FxMazter on December 25, 2002 9:07:34 AM]
Thank you Kylotan!

As I said... I have just started out with this... so plz don''t get mad at my stupid questions...
quote:Original post by FxMazter
Is it like that you only load a bitmap once during initialization... then you recieave a handle to the bitmap memory.

Yes... the problem is, when that function ends, you lose that handle. Meaning you have no way of accessing the bitmap memory you allocated using LoadImage. So that memory is wasted until the program ends. And you add another wasted bitmap each time you call that function as a result.

The key is to keep a handle to the bitmap at all times, from initialisation where you load it, to shutdown where delete it.

quote:Then every time you want to show that bitmap, you blit it to the ddsurface? And this way there is only one memory allocation?

That''s right... all you''re trying to do is overwrite screen memory with the contents of that bitmap memory. That''s what blitting is about. You don''t need to reload anything to do this, just copy the data from one place to another each frame.

Happy to help.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

This topic is closed to new replies.

Advertisement