Thread: Memory Leak?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    80

    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

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Have you tried using codeguard? It’s a tool within the builder environment that will detect and diagnose memory leaks.
    Be a leader and not a follower.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    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.

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    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
    ==========================================
    Be a leader and not a follower.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    This looks very useful...does anyone know how to apply it to Visual Studio? That would be great!

    Thanks

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    I have Borland now...now how do I use codeguard?

    Thanks!

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    Thanks, I'll try that right now.

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    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.

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    Thanks Shiro,

    Do I have to include any directories for that? What is the output like? Thanks!

    Andrew

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    Thanks guys,

    I got my memory leak fixed...Kind of a dumb error, actually...but that's how it often goes, eh?

    Andrew

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  4. Any Memory Leak Checking Tool?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 11:02 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM