|
-
March 30th, 2009, 08:29 AM
#1
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!!
-
March 30th, 2009, 08:43 AM
#2
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.
-
March 30th, 2009, 03:27 PM
#3
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
-
March 30th, 2009, 05:04 PM
#4
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.
-
March 31st, 2009, 02:01 AM
#5
Re: Detecting memory leak in complicating source code
 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 :
Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.
-
March 31st, 2009, 08:28 AM
#6
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?
-
May 4th, 2010, 04:25 AM
#7
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|