Jump to content
  • Advertisement

Archived

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

JohanOfverstedt

Memory leaks in the win api

This topic is 5430 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


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

Share this post


Link to post
Share on other sites
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);

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 function

SelectObject(dc, old);

I think this illustrates what TempusElf said.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!