Memory leaks in the win api

Started by
16 comments, last by JohanOfverstedt 20 years, 8 months ago
I am writing an application that uses the win api a lot, and I am concerned that I might be leaking memory, by not releasing something, that I should release, or some other stuff. I wonder if there are any (preferably free) programs/librarys that allow me to find out, if I am leaking memory through another library(like the win api). Thanks in advance! My game: Swift blocks
DISCLAIMER: If any of the above statements are incorrect, feel free to deliver me a good hard slap!My games: DracMan | Swift blocks
Advertisement
Fluid Studio''s MemMgr is great for memory.

If you''re worried about leaking Win32 resources, then I''d suggest using some wrapper classes to automatically take care of the cleanup for you. A lot of those are provided using WTL or MFC (ugh).

Or, you could just turn around and use .NET WinForms.
daerid@gmail.com
you probably already know this but just in case:

according to winprog.org, using SelectObject can potentially cause memory leaks if you don''t catch what it returns and get rid of it (assuming that you dont need it anymore)

for example if you''re constantly creating new brushes and only using them once you should always

bTemp = (HBRUSH)SelectObject(dcWhatever, bNewBrush);DeleteObject(bTemp); 
Is there a tool of somekind that monitors and can tell you that memory has leaked when you quit your program?

- CheeseMonger
- CheeseMonger
daerid:

Yes, I know about that memory manager, and I use it already!
And, none of the wrapper classes seem like good options to me

TempusElf:

Thanks for that information, I actually didn''t know that
I look more at that site, to see what else I can dig up!

CheeseMonger:

Yes, something like that, is what I am looking for too.
A lib or app that tells med that!

Thanks everybody for your responses, please keep on posting advice, and maybe links and stuff

Cheers

My game: Swift blocks
DISCLAIMER: If any of the above statements are incorrect, feel free to deliver me a good hard slap!My games: DracMan | Swift blocks
quote:Original post by TempusElf
for example if you''re constantly creating new brushes and only using them once you should always

bTemp = (HBRUSH)SelectObject(dcWhatever, bNewBrush);
DeleteObject(bTemp);


Where did you learn this? According to the MSDN this is wrong.

quote:MSDN on SelectObject()
An application should always replace a new object with the original, default object after it has finished drawing with the new object.



Qui fut tout, et qui ne fut rien
Invader''s Realm
I came across this problem today.

The correct way to avoid leaking (which doesn''t occur on Win2k,
but does on WinXP - I cannot tell about non-NT platforms) is
handling SelectObject calls like this:
HGDIOBJ old = SelectObject(dc, obj);// use BitBlt or whatever GDI functionSelectObject(dc, old);

I think this illustrates what TempusElf said.
Look for "_CrtDumpMemoryLeaks" in the online help of your Visual Studio.

quote:Original post by Invader X

quote:MSDN on SelectObject()
An application should always replace a new object with the original, default object after it has finished drawing with the new object.



I see what you mean but consider this:

What if I'm done with the new object, but now I want to draw with an even newer object... so I go and use SelectObject again to put the more recent object in the DC and replace the one that is there... now what happens to that one? does windows detect that I have no reference to it and get rid of it? I don't think it does (not that I'm an expert on this)

I think that you and I are talking about different situations... what I'm talking about is something like this:
HBITMAP hbmNewBitmap;while(gameon){  //create a new GDI object  hbmNewBitmap = CreateCompatibleBitmap(dcSomeDC, someheight, somewidth);  //put new object into dc... replace the old  //ignoring the returned pointer to the old bitmap  SelectObject(dcSomeDC, hbmNewBitmap);}  


It seems to me that the code I just wrote is like doing this:

cFooBar fbNew;while(gameon){  fbNew = new cFooBar();}  


an obvious memory leak because you aren't deallocating fbNew when you're done with it... I could be wrong...

[edited by - TempusElf on August 8, 2003 6:53:39 PM]
I have tried the _CrtDumpMemoryLeaks but strangly never got it to work as expected.

I will try it again this night, and see if I can put some life to it!

(EDIT)
I can only get the CRT MemManager to work, when I run the application from the VC++ 6.0 IDE.

Is there a way to keep that great mem manager even when I start my app from windows explorer?
(/EDIT)

Cheers

My game: Swift blocks

[edited by - JohanOfverstedt on August 8, 2003 8:31:09 PM]
DISCLAIMER: If any of the above statements are incorrect, feel free to deliver me a good hard slap!My games: DracMan | Swift blocks

This topic is closed to new replies.

Advertisement