Memory Leak

Discussion in 'C Programming' started by GJ, Oct 26, 2005.

  1. GJ

    GJ Guest

    Hi,

    Can we trace a memory leak in a C program without using any specific tools
    like purifier.

    --
    GJ
     
    GJ, Oct 26, 2005
    #1
    1. Advertising

  2. GJ wrote:
    > Hi,
    >
    > Can we trace a memory leak in a C program without using any specific tools
    > like purifier.
    >


    Yes, you can trace the program yourself with a pencil a lots of paper.
     
    Antonio Contreras, Oct 26, 2005
    #2
    1. Advertising

  3. GJ

    makuchaku Guest

    Hi GJ,

    You can override the malloc/free functions in your scope & detect leaks
    in free.
    Each malloc call should be free'd in free. If not, flag a memory leak...
     
    makuchaku, Oct 26, 2005
    #3
  4. makuchaku <> wrote:

    > You can override the malloc/free functions in your scope & detect leaks
    > in free.
    > Each malloc call should be free'd in free. If not, flag a memory leak...


    Memory is not "leaked" until all pointers to the memory (besides that
    stored in your proposed memory management system) are gone. How do
    you propose to verify that your memory management system has the one
    and only pointer to some block of memory?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 26, 2005
    #4
  5. GJ

    Jordan Abel Guest

    On 2005-10-26, Christopher Benson-Manica <> wrote:
    > makuchaku <> wrote:
    >
    >> You can override the malloc/free functions in your scope & detect leaks
    >> in free.
    >> Each malloc call should be free'd in free. If not, flag a memory leak...

    >
    > Memory is not "leaked" until all pointers to the memory (besides that
    > stored in your proposed memory management system) are gone. How do
    > you propose to verify that your memory management system has the one
    > and only pointer to some block of memory?


    What you'd need is a malloc substitute that records where and possibly when
    each block was originally allocated, and then at program end, list the ones
    that aren't freed. This will end up with false positives, particularly in case
    of an exit other than returning from main(), but this information will give you
    at least a place to start looking
     
    Jordan Abel, Oct 26, 2005
    #5
  6. GJ

    Netocrat Guest

    On Wed, 26 Oct 2005 11:28:05 +0000, Jordan Abel wrote:

    > On 2005-10-26, Christopher Benson-Manica <> wrote:
    >> makuchaku <> wrote:
    >>
    >>> You can override the malloc/free functions in your scope & detect leaks
    >>> in free.
    >>> Each malloc call should be free'd in free. If not, flag a memory leak...

    >>
    >> Memory is not "leaked" until all pointers to the memory (besides that
    >> stored in your proposed memory management system) are gone. How do
    >> you propose to verify that your memory management system has the one
    >> and only pointer to some block of memory?


    Right - it doesn't seem to be possible even with implementation support -
    a pointer's value could be stored in a format not recognised as such by
    the implementation.

    > What you'd need is a malloc substitute that records where and possibly when
    > each block was originally allocated, and then at program end, list the ones
    > that aren't freed.


    I've written something similar but simpler for my personal debugging use.
    It consists of a malloc() wrapper - chkmalloc() - that stores the address
    and size of memory returned by successful calls to malloc in a node in a
    tree (using tsearch() - not part of the C Standard but provided by my
    implementation), as well as a free() wrapper - chkfree() - that removes
    that node from the tree, and a few functions for printing out currently
    allocated memory based on the tree.

    All it requires is discipline to use only the modified functions (easily
    confirmed with OS-specific search tools like grep), which in production
    code can be redefined as macros expanding to malloc() and free().

    It would be easy to extend it to record where the block was allocated
    using the __func__, __LINE__ and __FILE__ macros, and to record when using
    the time() function, but I haven't found need for that yet.

    > This will end up with false positives, particularly
    > in case of an exit other than returning from main(), but this


    I haven't come across any other situations. Do you have any in mind
    specifically?

    > information will give you at least a place to start looking


    I've collected some links to related tools, none of which I've yet used
    and some of which may suit the OP's purpose:

    ftp://ftp.perens.com/pub/ElectricFence/
    http://dickey.his.com/dbmalloc/
    http://dmalloc.com/
    http://valgrind.org/
    http://www.gnu.org/software/checker/checker.html

    --
    http://members.dodo.com.au/~netocrat
     
    Netocrat, Oct 26, 2005
    #6
    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. =?Utf-8?B?Y3liZXJzdHJpa2U=?=

    datagrid memory leak?

    =?Utf-8?B?Y3liZXJzdHJpa2U=?=, Jan 3, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    466
    =?Utf-8?B?Y3liZXJzdHJpa2U=?=
    Jan 3, 2005
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    707
    Eric Sosman
    Mar 22, 2005
  3. Richard Heathfield

    Leak or no leak ??

    Richard Heathfield, Jul 10, 2006, in forum: C Programming
    Replies:
    4
    Views:
    353
    Richard Heathfield
    Jul 10, 2006
  4. cham
    Replies:
    5
    Views:
    772
  5. Mark Probert
    Replies:
    4
    Views:
    331
    Mark Probert
    Feb 9, 2005
Loading...

Share This Page