Detecting memory leak in complicating source code
Hello everyone,
I'am trying to run a code which performs continuously min-cuts to a graph until it detects dense subgraphs.
Unfortunately, it seems that there is a memory leak and the program exits. As the program is constituted of many files, it is really difficult to detect the memory leak error.
I was wondering if there is a way to print the size of the allocated memory after each min-cut in order to test my code and find the memory leak.
Thank you!!
Re: Detecting memory leak in complicating source code
Normally the Visual Studio tells you (in debug mode) that you are leaking memory, and when you are using new/delete it will even tell you where you are leaking it.
Re: Detecting memory leak in complicating source code
Hi,
You can make use of CMemoryState class
Code:
CMemoryState state1, state2, memDiff;
state1.Checkpoint();
....
....
....
state2.Checkpoint();
if ( memDiff.Difference ( state1, state2 ) )
{
//..LEAKED!!!
}
Hope it helps..
Thanks,
dwurity
Re: Detecting memory leak in complicating source code
The absolute easiest way to locate memory leaks is to use a tool like valgrind (Linux). I don't know of a Windows equivalent which is quite as easy, at least not a free one. The above will work if you're willing to make source code changes.
Re: Detecting memory leak in complicating source code
Quote:
Originally Posted by
Lindley
The absolute easiest way to locate memory leaks is to use a tool like valgrind (Linux). I don't know of a Windows equivalent which is quite as easy, at least not a free one. The above will work if you're willing to make source code changes.
wake up and check where you posted this comment in :
Quote:
Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.
Re: Detecting memory leak in complicating source code
Thank you guys for your responce!
@dwurity,
how can I use CMemoryState? I included in my code but i receive a code error. Should i include any library or something like this?
Re: Detecting memory leak in complicating source code
The memory leak information is printed on "Output" window.
[ This advantage is in "Debug" mode only. ]
Example
int _tmain()
{
int* ptr = NULL;
ptr = (int*)malloc(sizeof(int)*20);
for( int i=0; i<20; ++i )
ptr[i] = i;
#ifndef NDEBUG
int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Get current flag
flag |= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit
_CrtSetDbgFlag(flag); // Set flag to the new value
#endif
//free(ptr);
printf("\n\nPress Enter...");
getch();
return 0;
}
http://www.mindfiresolutions.com/Det...g-mode-749.php