Sign in to follow this  
CodaKiller

Why can't I delete this array?!

Recommended Posts

CodaKiller    108
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.

Share this post


Link to post
Share on other sites
Cecil_PL    122
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.

Share this post


Link to post
Share on other sites
CodaKiller    108
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?

Share this post


Link to post
Share on other sites
CodaKiller    108
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?!

Share this post


Link to post
Share on other sites
Evil Steve    2017
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).

Share this post


Link to post
Share on other sites
CodaKiller    108
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.

Share this post


Link to post
Share on other sites
Evil Steve    2017
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]

Share this post


Link to post
Share on other sites
CodaKiller    108
Quote:
Original post by Evil Steve
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.


I added it to my project and defined USE_MMGR, is that all I need to do?

Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by CodaKiller
I added it to my project and defined USE_MMGR, is that all I need to do?
And rebuild, yes. The USE_MMGR define is automatically defined for debug builds.

Note that this only tracks allocations from new or new[] - although malloc / realloc shouldn't really be used in C++ anyway.

Share this post


Link to post
Share on other sites
CodaKiller    108
Quote:
Original post by Evil Steve
Quote:
Original post by CodaKiller
I added it to my project and defined USE_MMGR, is that all I need to do?
And rebuild, yes. The USE_MMGR define is automatically defined for debug builds.

Note that this only tracks allocations from new or new[] - although malloc / realloc shouldn't really be used in C++ anyway.


Wow that's very useful! Thanks!

Share this post


Link to post
Share on other sites

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