Jump to content
  • Advertisement
Sign in to follow this  
waZim

Memory Leak in Win32 applications

This topic is 3436 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

Hi All, I have been observing the Win32 Simple Application from http://www.winprog.org/tutorial/simple_window.html NOTE : For some one who wanna help, must go to that link and create a win32 application from this tutorial. And it seems there is a memory leak in the application, which does not makes any sense, since its a simple Win32 Application and nothing is happing complicated inside. when i observe the memory requirements in Task Manager it stays relatively constant, but when i move the window or move my mouse on top of it, resize it or just move it around, restore etc. the memory requirement fluctuates and goes up. any one have any idea of whats going on and can we control that? is there inherited memory leaks in win32 API? Any suggestions, help would be greatly appriciated. .. waZim

Share this post


Link to post
Share on other sites
Advertisement
No. The task manager is not a suitable tool for observing memory consumption in this fashion. It doesn't tell you what you think it tells you. You will need to find a profiling tool or library that can show you more relevant memory statistics (and read the documentation to understand what they mean) in order to make any such judgements.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
No. The task manager is not a suitable tool for observing memory consumption in this fashion. It doesn't tell you what you think it tells you.


So whats the best way of varifying? and checking for memory consumption and leaks.

Share this post


Link to post
Share on other sites
Typically libraries are used that hook the allocation and deallocation routines in your program. The program is recompiled with those hooks enabled, and the library provides functionality that lets you know what memory is outstanding at a given point in time, which can be used to determine if you leak during application shutdown or during execution at a specific point you determine, et cetera.

In C++, preventative measures are often taken by avoiding manual memory management where possible, preferring idioms like RAII, things like smart pointers and SC++L containers, et cetera.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Typically libraries are used that hook the allocation and deallocation routines in your program. The program is recompiled with those hooks enabled, and the library provides functionality that lets you know what memory is outstanding at a given point in time, which can be used to determine if you leak during application shutdown or during execution at a specific point you determine, et cetera.

In C++, preventative measures are often taken by avoiding manual memory management where possible, preferring idioms like RAII, things like smart pointers and SC++L containers, et cetera.


Thanks again,
Yes i have a customed type Memory manager "sort of" which tells me about where i have been doing new calls and not calling deletes on those addresses,

But since i can't see my customed memory manager output for the simple win32 application, for all other taskes my classes inherits from my memory manager and it shows me any "new" and "delete" calls on those object etc.

But on the core API?
And Task Manager kinda scared me, since the Memory requrements were fluctuating.

Share this post


Link to post
Share on other sites
Maybe an old post of mine could help you: Scroll down. There I listed some tools to track memory issues down using windows ( including the Win32-API allocations ). Just at the end of the thread you can found them.

Kimmi

[Edited by - Kimmi on March 17, 2009 1:18:59 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by waZim
Quote:
Original post by jpetrie
Typically libraries are used that hook the allocation and deallocation routines in your program. The program is recompiled with those hooks enabled, and the library provides functionality that lets you know what memory is outstanding at a given point in time, which can be used to determine if you leak during application shutdown or during execution at a specific point you determine, et cetera.

In C++, preventative measures are often taken by avoiding manual memory management where possible, preferring idioms like RAII, things like smart pointers and SC++L containers, et cetera.


Thanks again,
Yes i have a customed type Memory manager "sort of" which tells me about where i have been doing new calls and not calling deletes on those addresses,

But since i can't see my customed memory manager output for the simple win32 application, for all other taskes my classes inherits from my memory manager and it shows me any "new" and "delete" calls on those object etc.

But on the core API?
And Task Manager kinda scared me, since the Memory requrements were fluctuating.


There are libraries built into the C runtime for detecting memory leaks automatically for you. If you wrap main with a construct like this:


void main()
{
#ifdef _DEBUG
_CrtMemState initMemoryState;
_CrtMemCheckPoint(&initMemoryState);
#endif

//rest of program goes here as normal

#ifdef _DEBUG
_CrtMemState finalMemoryState;
_CrtMemCheckpoint(&finalMemoryState);

_CrtMemState diffMemoryState;
if(_CrtMemDifference(&diffMemoryState, &initMemoryState, &finalMemoryState))
{
_CrtMemDumpStatistics(&diffMemoryState);
_CrtDumpMemoryLeaks();
DebugBreak();
}
#endif
}



This will then print out a report of any leaked memory (between the check point calls) into Visual Studio's output window. It will also tell you the allocation # of that memory, which if your program is deterministic, you can use _CrtSetBreakAlloc() to break on that allocation number and see exactly what is being leaked.

Share this post


Link to post
Share on other sites
Quote:
Original post by andur
Quote:
Original post by waZim
Quote:
Original post by jpetrie
Typically libraries are used that hook the allocation and deallocation routines in your program. The program is recompiled with those hooks enabled, and the library provides functionality that lets you know what memory is outstanding at a given point in time, which can be used to determine if you leak during application shutdown or during execution at a specific point you determine, et cetera.

In C++, preventative measures are often taken by avoiding manual memory management where possible, preferring idioms like RAII, things like smart pointers and SC++L containers, et cetera.


Thanks again,
Yes i have a customed type Memory manager "sort of" which tells me about where i have been doing new calls and not calling deletes on those addresses,

But since i can't see my customed memory manager output for the simple win32 application, for all other taskes my classes inherits from my memory manager and it shows me any "new" and "delete" calls on those object etc.

But on the core API?
And Task Manager kinda scared me, since the Memory requrements were fluctuating.


There are libraries built into the C runtime for detecting memory leaks automatically for you. If you wrap main with a construct like this:

*** Source Snippet Removed ***

This will then print out a report of any leaked memory (between the check point calls) into Visual Studio's output window. It will also tell you the allocation # of that memory, which if your program is deterministic, you can use _CrtSetBreakAlloc() to break on that allocation number and see exactly what is being leaked.



This seems greate if it really works, i am gonna check it tomorrow. my first try could not compile symbol _CrtMemCheckpoint ..

thanks

Share this post


Link to post
Share on other sites
Niiiiiiiiice,,

it works..

_CrtMemCheckpoint this was misspelled in your code. :)

..
waZim

Share this post


Link to post
Share on other sites
Quote:
Original post by waZim
Niiiiiiiiice,,

it works..

_CrtMemCheckpoint this was misspelled in your code. :)

..
waZim


Glad it works :) The debugger should really have something like this built into it. This won't catch everything though, I don't think it will catch any static variables that initialize stuff on the heap.

Yeah, sorry about that typo in typing it in here.

You can also call _CrtSetAllocHook and pass it a function that gets called whenever memory is allocated/freed/reallocated. MSDN explains pretty well how it works.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!