detecting memory leaks
What do you do to detect memory leaks within your program? What I do now is just run the program a few times and see if my computer freezes up or something, but I know that this can''t be good for my comp. Any suggestions?
-----------------------------Weeks of programming can save you hours of planning.There are 10 kinds of people in this world-- those who understand binary and those who don't.
August 07, 2002 06:37 PM
If you''re using C, just write a malloc front end. Use a linked list or an array of pointers, whatever you''re comfortable with.
I usually use something like this:
struct aNode {
void *ptr;
int size;
} memindex[512];
Initialize the array by setting ptr to NULL and size to 0, then write some functions around it:
ie:
void *_malloc(ssize_t size) {
void *ptr;
ptr = malloc(size);
if (!ptr) {
return(NULL);
}
#ifdef DEBUG
mfWrlog("Allocating %d bytes of memory, %d total used", size);
#endif
mfAddElement(ptr, size);
mfCurrentUsage += size;
return(ptr);
}
void _free() {
int size;
#ifdef DEBUG
free(ptr);
size = mfGetElementSize(ptr);
mfWrlog("Freeing %d bytes of memory, %d total used", size);
mfCurrentUsage -= size;
mfDelElement(ptr);
return();
}
Just fill in mfAddElement(void *, int), mfGetElementSize(void *), and mfDelElement(void *). mfCurrentUsage can just be an unsigned int. You should also write an mfCleanUp() function, which will just iterate through the array, it should look something like this:
int mfCleanUp() {
int x = 0;
while (x < 512) {
if (memindex[x].ptr) {
mfUnClean += mfGetElementSize(ptr);
_free(memindex[x].ptr);
}
x++;
}
return(mfUnClean);
}
Call it right before your program exits. The return value will be how many bytes your program didn''t free. Or you could do the wrLog thing in the function that calls _malloc() and _free(). It will be easier to track that way.
I usually use something like this:
struct aNode {
void *ptr;
int size;
} memindex[512];
Initialize the array by setting ptr to NULL and size to 0, then write some functions around it:
ie:
void *_malloc(ssize_t size) {
void *ptr;
ptr = malloc(size);
if (!ptr) {
return(NULL);
}
#ifdef DEBUG
mfWrlog("Allocating %d bytes of memory, %d total used", size);
#endif
mfAddElement(ptr, size);
mfCurrentUsage += size;
return(ptr);
}
void _free() {
int size;
#ifdef DEBUG
free(ptr);
size = mfGetElementSize(ptr);
mfWrlog("Freeing %d bytes of memory, %d total used", size);
mfCurrentUsage -= size;
mfDelElement(ptr);
return();
}
Just fill in mfAddElement(void *, int), mfGetElementSize(void *), and mfDelElement(void *). mfCurrentUsage can just be an unsigned int. You should also write an mfCleanUp() function, which will just iterate through the array, it should look something like this:
int mfCleanUp() {
int x = 0;
while (x < 512) {
if (memindex[x].ptr) {
mfUnClean += mfGetElementSize(ptr);
_free(memindex[x].ptr);
}
x++;
}
return(mfUnClean);
}
Call it right before your program exits. The return value will be how many bytes your program didn''t free. Or you could do the wrLog thing in the function that calls _malloc() and _free(). It will be easier to track that way.
Hi
the easiest way is to use Fluidstudio''s Memory Manager:
MMGR. It''s on the bottom of the page.
Another way (that works at least in VC++, not sure about other compilers):
include crtdbg.h in your project, and at the beginning of the (Win)main write this:
If you now run a debug session ( hit F5 in VC++, don''t just compile in debug mode), you will see it in the output window if there are mem leaks. But this only tells you if there are leaks, how big they are and the contents, but not the code line numbers where they originated. But it''s a quick way to just check if your code is clean.
------------
Runicsoft
the easiest way is to use Fluidstudio''s Memory Manager:
MMGR. It''s on the bottom of the page.
Another way (that works at least in VC++, not sure about other compilers):
include crtdbg.h in your project, and at the beginning of the (Win)main write this:
int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);flag |= _CRTDBG_LEAK_CHECK_DF; _CrtSetDbgFlag(flag);
If you now run a debug session ( hit F5 in VC++, don''t just compile in debug mode), you will see it in the output window if there are mem leaks. But this only tells you if there are leaks, how big they are and the contents, but not the code line numbers where they originated. But it''s a quick way to just check if your code is clean.
------------
Runicsoft
Could you also overload the global new and delete operators? is this a good idea?
CEO Platoon Studios
CEO Platoon Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
I just use the quick n easy _CrtDumpMemoryLeaks (). You can put it in your WinMain like this...
Since the game object goes out of scope, it cleans itself up. There should therefore be no allocated memory when execution goes outside the block to the _CrtDumpMemoryLeaks function.
It depends on your code whether this will work or not. It won't if you've got global classes that allocate memory, but I don't.
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
[edited by - siaspete on August 8, 2002 8:54:42 AM]
#include <windows.h>#include <crtdbg.h>int WinMain (blah){ int iResult = 0; { // ALL your app code goes here // Don't allocate anything outside this block! Game game; iResult = game.Run (); } _CrtDumpMemoryLeaks (); return iResult;}
Since the game object goes out of scope, it cleans itself up. There should therefore be no allocated memory when execution goes outside the block to the _CrtDumpMemoryLeaks function.
It depends on your code whether this will work or not. It won't if you've got global classes that allocate memory, but I don't.
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
[edited by - siaspete on August 8, 2002 8:54:42 AM]
I also recommend _CrtDumpMemoryLeaks() (and the other _Crt* functions are worth looking into). To get the file and line number of the malloc() call that caused the leak, you''ll need something like:
When _CrtDumpMemoryLeaks() displays any leak information, the file and line will be displayed. Double-clicking these lines should open the corresponding file in the source editor and jump to the line the leak occurred in.
I don''t think that would freeze your computer unless it''s a complete POS.
"If people are good only because they fear punishment and hope for reward, then we are a sorry lot indeed." - Albert Einstein
#ifdef _DEBUG // MSVC-specific.
#define malloc(x) _malloc_dbg(x,_NORMAL_BLOCK,__FILE__,__LINE__)
#endif
When _CrtDumpMemoryLeaks() displays any leak information, the file and line will be displayed. Double-clicking these lines should open the corresponding file in the source editor and jump to the line the leak occurred in.
quote: What I do now is just run the program a few times and see if my computer freezes up or something...
I don''t think that would freeze your computer unless it''s a complete POS.
"If people are good only because they fear punishment and hope for reward, then we are a sorry lot indeed." - Albert Einstein
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement