Memory Leak in Win32 applications

Started by
15 comments, last by waZim 15 years, 1 month ago
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
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.
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.
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.
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.
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]
A complicate solution may indicate a not understood problem.


[twitter]KimKulling[/twitter]
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.
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
Niiiiiiiiice,,

it works..

_CrtMemCheckpoint this was misspelled in your code. :)

..
waZim
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.

This topic is closed to new replies.

Advertisement