-
Memory Leak?
Hi guys,
Recently I wrote a template linked list class, iterator class, node class, etc. These are for use in a program that keeps track of meetings, and the participants in the meetings. The client program has a 2 lists: A list of objects of type meeting (Another class I wrote) and a list of pointers to objects of type person (Another class I wrote). The program keeps track of which "people" are invloved in which "meeting" by a private list in the meeting class. This list is called the "participants" list. This list contains pointers to the elements in the main "people" list. As I add/remove participants from meetings, the "participants" list is updated with pointers to the "people" list.
My programs are graded by an autograder software, and the only test that my program fails is the test of memory allocation, so I am led to believe there is a memory leak somewhere. My list class contains pop() and erase() functions that appear to work perfectly, and deallocate memory correctly. Thus, I think the problem lies in my client program. There, if I need to delete a person from the main "people" list, I must iterate through the list, find the person to delete, delete the pointer to the person, set the person* = NULL, and then finally remove the node from the list.
Does anything at all sound wrong with my logic?
Sorry for the long post...I was going to post code, but that would only icrease the length! If any code is needed, I can easily supply.
Thanks!
Andrew
-
Have you tried using codeguard? It’s a tool within the builder environment that will detect and diagnose memory leaks.
-
Thanks subdene. This sounds like something I need! I use MSVC++ 6.0...what do I need to know from there?
Also, in the meantime, if others have ideas also, please chime in.
-
To be honest I’m not that familiar with msvc++, but if you can get a copy of the Borland, codeguard is extremely useful. I created a dynamic array which was never deleted and codeguard generated the following log....
Error 00001. 0x300010 (Thread 0x0864):
Resource leak: The object array (0xCF5E90) was never deleted
The object array (0x00CF5E90) [size: 400 bytes] was created with new[]
Call Tree:
0x004018B3(=Project1.exe:0x01:0008B3) D:\Documents and Settings\Subdene\Desktop\Unit1.cpp#20
0x40053323(=VCL50.BPL:0x01:052323)
0x4005301F(=VCL50.BPL:0x01:05201F)
0x4005ABFC(=VCL50.BPL:0x01:059BFC)
0x00401455(=Project1.exe:0x01:000455) D:\Documents and Settings\Subdene\Desktop\Project1.cpp#13
0x3257DBEF(=CC3250MT.DLL:0x01:07CBEF)
------------------------------------------
Functions called:
delete (35 times)
SysFreeMem (280 times)
SysGetMem (280 times)
realloc (1 times)
delete[] (2 times)
free (26 times)
new[] (15 times)
new (40 times)
calloc (5 times)
malloc (20 times)
Resource types used:
object array (15 allocs, 14 max)
object (40 allocs, 28 max)
memory block (306 allocs, 229 max)
Modules used:
00400000 12/06/2002 21:58:10 D:\Documents and
Settings\Subdene\Desktop\Project1.exe
0CD00000 01/31/2000 05:00:00 D:\PROGRA~1\Borland\CBUILD~1\Bin\CG32.DLL
10000000 09/11/2002 09:13:22 D:\WINDOWS\system32\SSSensor.dll
32500000 01/31/2000 05:00:00 D:\PROGRA~1\Borland\CBUILD~1\Bin\CC3250MT.DLL
40000000 01/24/2000 05:01:00 D:\WINDOWS\System32\VCL50.BPL
41000000 01/31/2000 05:00:00
D:\PROGRA~1\Borland\REMOTE~1\5.0\Bin\BORLNDMM.DLL
5AD70000 08/23/2001 20:00:00 D:\WINDOWS\system32\uxtheme.dll
63000000 04/01/2002 15:51:42 D:\WINDOWS\System32\SynTPFcs.dll
67F00000 11/26/2001 19:31:36 D:\Program Files\AOL 7.0\IDLEPROC.DLL
70BD0000 08/29/2002 07:14:40 D:\WINDOWS\system32\SHLWAPI.dll
71950000 08/23/2001 20:00:00
D:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.
0.0_x-ww_1382d70a\comctl32.dll
71AA0000 08/23/2001 20:00:00 D:\WINDOWS\System32\WS2HELP.dll
71AB0000 08/23/2001 20:00:00 D:\WINDOWS\System32\WS2_32.dll
71AD0000 08/23/2001 20:00:00 D:\WINDOWS\System32\wsock32.dll
71B20000 08/23/2001 20:00:00 D:\WINDOWS\system32\mpr.dll
73000000 08/23/2001 20:00:00 D:\WINDOWS\System32\winspool.drv
74720000 08/23/2001 20:00:00 D:\WINDOWS\System32\MSCTF.dll
74D30000 08/23/2001 20:00:00 D:\WINDOWS\System32\oledlg.dll
763B0000 08/23/2001 20:00:00 D:\WINDOWS\system32\comdlg32.dll
77120000 08/23/2001 20:00:00 D:\WINDOWS\system32\oleaut32.dll
771B0000 08/23/2001 20:00:00 D:\WINDOWS\system32\OLE32.DLL
77340000 08/23/2001 20:00:00 D:\WINDOWS\system32\comctl32.dll
773D0000 08/23/2001 20:00:00 D:\WINDOWS\system32\SHELL32.dll
77C00000 08/23/2001 20:00:00 D:\WINDOWS\system32\version.dll
77C10000 08/23/2001 20:00:00 D:\WINDOWS\system32\MSVCRT.DLL
77C70000 08/23/2001 20:00:00 D:\WINDOWS\system32\GDI32.dll
77CC0000 08/23/2001 20:00:00 D:\WINDOWS\system32\RPCRT4.dll
77D40000 08/23/2001 20:00:00 D:\WINDOWS\system32\user32.dll
77DD0000 08/23/2001 20:00:00 D:\WINDOWS\system32\ADVAPI32.dll
77E60000 08/23/2001 20:00:00 D:\WINDOWS\system32\kernel32.dll
77F50000 01/21/2001 06:54:18 D:\WINDOWS\System32\ntdll.dll
==========================================
-
This looks very useful...does anyone know how to apply it to Visual Studio? That would be great!
Thanks
-
I have Borland now...now how do I use codeguard?
Thanks!
-
You can use M$'s memory leak checker. It probably isn't as good as Codeguard but it is free :)
The header file regarding it is CRTDBG.H
Here is a quick way to check if you have leaks ONLY IN DEBUG MODE. There are tons of things you can do but I'll post the simplest and easiest.
Code:
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
So add this to the top of your main function. Then run your program by using "GO". Then when you exit it will produce output in the debug window if you have memory leaks. It will tell you how many bytes etc etc. It has some advanced features but I'll leave it up to you to find them out. All of them are documented on MSDN.
-
Thanks, I'll try that right now.
-
When using MSVC++, you can use the class CMemoryState for detecting memory leakages.
Code:
CMemoryState memState;
memState.Checkpoint ();
YourFunction ();
memState.DumpAllObjectsSince ();
Note that this only works if you use the new and delete operators, it will not work with malloc or free.
-
Thanks Shiro,
Do I have to include any directories for that? What is the output like? Thanks!
Andrew
-
It's probably just a wrapper for the header I showed you. Look on MSDN and you can find how to set the checkpoints manually. Also there are some tricks where it will tell you the file and line the memory was allocated on AND another trick to double click it and go to that line.
http://216.239.53.100/search?q=cache...hl=en&ie=UTF-8
http://www.cs.wisc.edu/~tlabonne/memleak.html
-
Thanks guys,
I got my memory leak fixed...Kind of a dumb error, actually...but that's how it often goes, eh?
Andrew