Why can't I delete this array?!

Started by
11 comments, last by CodaKiller 15 years, 4 months ago
Well I can't seem to delete this array, I'm having a memory leak and I believe that it's the reason, here is the code:

		HDC hDC = CreateCompatibleDC(NULL);
		byte* pSrcData;
		BITMAPINFO bmi = { sizeof( BITMAPINFOHEADER ), rect.right, rect.bottom, 1, 32, BI_RGB, 0, 0, 0, 0, 0};
		HBITMAP hTempBmp = CreateDIBSection( hDC, &bmi, DIB_RGB_COLORS, (void**)&pSrcData, NULL, 0 );
		HBITMAP hOldBmp = (HBITMAP)SelectObject(hDC, hTempBmp );
		HFONT NewFont = CreateFont( FontSize, 0, 0, 0, (FW_BOLD * bold), italic, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 0, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, font.c_str() );
		HBRUSH NewBrush = CreateSolidBrush(0);
		SelectObject(hDC, NewFont);
		SelectObject(hDC, NewBrush);
		SetTextColor( hDC, RGB(0,0,255));
		SetBkColor( hDC, 0);
		DrawText(hDC, text.c_str(), text.size(), &rect, DT_LEFT | DT_WORDBREAK | DT_NOPREFIX);
		DeleteObject(NewBrush);
		DeleteObject(NewFont);	
		ReleaseDC(NULL, hDC);
//... This is where I copy it to a texture
//... This is where I try "delete [] pSrcData;" then it gives the error.

I get an access violation when I try to delete pSrcData, I know it has data in it since I am copying it to a texture and can see it.
Remember Codeka is my alternate account, just remember that!
Advertisement
You don't have an array. You have a pointer.

You can only delete an array (or anything, for that matter) if you've newed it, which you don't seem to have done in this case.
http://msdn.microsoft.com/en-us/library/ms532292(VS.85).aspx

ppvBits
[out] Pointer to a variable that receives a pointer to the location of the DIB bit values.

It's not allocating memory for you to deallocate later. It's just giving you the pointer to a memory space that windows is managing.

-me
Quote:Original post by Palidine
http://msdn.microsoft.com/en-us/library/ms532292(VS.85).aspx

ppvBits
[out] Pointer to a variable that receives a pointer to the location of the DIB bit values.

It's not allocating memory for you to deallocate later. It's just giving you the pointer to a memory space that windows is managing.

-me


Well I know something in that code is creating an over flow when run in a loop. Can anyone see what it is?
Remember Codeka is my alternate account, just remember that!
Are you
DeleteObject(hTempBmp);
??
Check here

Quote:Original post by Crow-knee
Are you
DeleteObject(hTempBmp);
??
Check here


Didn't fix it, even these two lines of code create a 25K memory leak:

HDC hDC = CreateCompatibleDC(NULL);
ReleaseDC(NULL, hDC);

Why?!
Remember Codeka is my alternate account, just remember that!
Aha! You can't use ReleaseDC on a DC that was created, you have to use DeleteDC.

I love the good old MSDN docs! [smile]
Remember Codeka is my alternate account, just remember that!
Quote:Original post by CodaKiller
Quote:Original post by Crow-knee
Are you
DeleteObject(hTempBmp);
??
Check here


Didn't fix it, even these two lines of code create a 25K memory leak:

HDC hDC = CreateCompatibleDC(NULL);
ReleaseDC(NULL, hDC);

Why?!
How are you determining that's a memory leak? Task Manager is not a suitable tool for that, it doesn't tell you anything useful about your application's memory usage (The "Working Set" is the working set size, not the allocated memory).
Quote:Original post by Evil Steve
Quote:Original post by CodaKiller
Quote:Original post by Crow-knee
Are you
DeleteObject(hTempBmp);
??
Check here


Didn't fix it, even these two lines of code create a 25K memory leak:

HDC hDC = CreateCompatibleDC(NULL);
ReleaseDC(NULL, hDC);

Why?!
How are you determining that's a memory leak? Task Manager is not a suitable tool for that, it doesn't tell you anything useful about your application's memory usage (The "Working Set" is the working set size, not the allocated memory).


So how should I be checking this? Also I know for a fact that it is creating a memory leak. As I said earlier I fixed it and the reason is quite simple, I needed to Delete the DC and not release control of it.
Remember Codeka is my alternate account, just remember that!
Quote:Original post by CodaKiller
So how should I be checking this? Also I know for a fact that it is creating a memory leak. As I said earlier I fixed it and the reason is quite simple, I needed to Delete the DC and not release control of it.
Well, yes it's a memory leak, but Task Manager isn't suitable for checking for memory leaks.

You can check for handle leaks by using the GDI Objects column in Task Manager, and for memory leaks from your code by using a memory leak checker like mmgr or similar.

If you allocate 500MB of memory and then release it, the OS might realise that you've requested a lot of memory recently and decide to keep it handy for your app. In that case, your application would still show over 500MB used for it's working set, even though there's no actual leak. Similarly, Vista has Superfetch which will slowly allocate memory, even when it's doing nothing (Since otherwise that memory isn't being used) - I've seen people complain that Vista leaks memory because they look at task manager and see the memory usage going up and up.

EDIT: Link to my memory manager: MemMgr.h and MemMgr.cpp. Just drop those files into your project, do a full rebuild, and you'll get a report at shutdown time of any memory leaks.

[Edited by - Evil Steve on December 16, 2008 5:44:41 AM]

This topic is closed to new replies.

Advertisement