How to identify who cause memory leak

Discussion in 'C++' started by NewToCPP, Dec 8, 2005.

  1. NewToCPP

    NewToCPP Guest

    Is there any debugging mechanism to find out which part of the code is
    causing memory leak?
    NewToCPP, Dec 8, 2005
    #1
    1. Advertising

  2. NewToCPP

    Guest

    , Dec 8, 2005
    #2
    1. Advertising

  3. NewToCPP

    red floyd Guest

    NewToCPP wrote:
    > Is there any debugging mechanism to find out which part of the code is
    > causing memory leak?
    >


    Not in the C++ language itself. There are many third party utilities
    that do so, but they are dependent upon your platform.

    I suggest that you ask in a newsgroup more relevant to your platform,
    such as either one of the microsoft.public.* newsgroups or
    comp.unix.programmer, or whatever the mac programming newsgroups are.

    There will be developers there who have dealt with this sort of thing,
    and can tell you better what's available.

    *** ON THE OTHER HAND ***

    Many memory leaks can be avoided by not using dynamic memory if at all
    possible, or by using containers and smart pointers.

    e.g. If you have a pointer that points to a single instance, use the
    smart pointer of your choice. If you have a pointer to multiple
    elements, which you're using as a dynamic array, then try std::vector
    instead. You should also use std::vector instead of arrays. If you're
    allocating char* to use as strings, then you should be using std::string
    instead.

    By using these elements instead of trying to manage memory yourself, you
    will minimize your leakage, because these containers (string, vector)
    and smart pointers will manage the memory for you, freeing it when it is
    no longer needed.

    You should also google for the term RAII (or Resource Acquisition Is
    Initialization).
    red floyd, Dec 8, 2005
    #3
  4. NewToCPP

    Gabriel Guest

    NewToCPP wrote:
    > Is there any debugging mechanism to find out which part of the code is
    > causing memory leak?
    >


    It is not a debugging mechanism, but:
    If you work on Linux, use the "valgrind" tool. It's the best tool for
    this I know about. It works non-invasive (you don't need to change any
    code - it works on the executable), which is very nice.

    Gabriel

    --
    Who is General Failure and why is he reading my hard disk?
    Gabriel, Dec 9, 2005
    #4
  5. NewToCPP

    Axter Guest

    NewToCPP wrote:
    > Is there any debugging mechanism to find out which part of the code is
    > causing memory leak?


    You can use the leaktracker program which is a free program that has
    the source code available. With the leaktracker program you can run
    your program, and when your program exits, the leaktracker program will
    automatically write to a file all memory leaks detected, which includes
    line number, sourc file name, and even function name where the leak was
    created.
    The leaktracker program detects leaks created by new, new[], malloc,
    realloc, and much more. It also detects resource leaks created by C
    functions like fopen.
    For windows platforms, it also has resource leak detection for windows
    API functions that use HANDLE type.

    Check out the following link:
    http://code.axter.com/leaktracker.h


    If you're target is windows, you can use the above link along with the
    DLL and Lib file in the following zip file to track memory leaks:
    http://code.axter.com/leaktracker.zip

    The program is free, and if you need the source code you can get it via
    following link:
    http://code.axter.com/leaktracker_dll_source_code.zip

    The code has been tested in Windows environment with MS VC++ 6.0, 7.1,
    GNU 3.x, and Borland 5.5.

    Although it's targeted for Windows, it wouldn't be that hard to port it
    to other platforms, since the Windows related part of the code has
    #ifdef WIN32 wrap.

    For more information, read the entire commented section of the
    leaktracker.h file.

    I'm also open to any suggestions to make the program better, or more
    portable.
    Axter, Dec 9, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antonio
    Replies:
    1
    Views:
    1,068
    Alvin Bruney [MVP - ASP.NET]
    Jun 29, 2005
  2. Jeff Rodriguez

    Can realloc potentially cause a memory leak?

    Jeff Rodriguez, Nov 19, 2003, in forum: C Programming
    Replies:
    4
    Views:
    312
    Derk Gwen
    Nov 19, 2003
  3. Yi
    Replies:
    6
    Views:
    286
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Apr 18, 2007
  4. Would this cause a memory leak?

    , Aug 1, 2007, in forum: C Programming
    Replies:
    11
    Views:
    606
  5. Obnoxious User

    Will this cause a memory leak?

    Obnoxious User, Apr 2, 2008, in forum: C++
    Replies:
    4
    Views:
    245
    Sharad
    Apr 3, 2008
Loading...

Share This Page