how to locate memory error

Discussion in 'C++' started by John Black, Sep 8, 2004.

  1. John Black

    John Black Guest

    Hi,
    In debugging a core dump, I run purify against my code, it says
    several "Free Memory Read" errors. All the errors are on one delete
    statement. Usually this means some pointer is deleted by non-owner. But
    it is really hard to locate the error code.

    The code structure is like this:

    class BigClass{ // this is from somebody else code
    ......
    }

    // following is my code

    BigClass* big = new BigClass();

    ... operation on big ...

    delete big;

    The core dump is on "delete big;", and purify says that it trying to
    free 4 bytes at 0x123456 which passes 85 bytes of a freed block, and
    that block is generated by func1().

    The problem is func1() is a very basic function which is called all
    over the code, so I have no clue to know the high level constructor.

    Wondering if you have any suggestion in catch such kind of error.

    Thanks.
     
    John Black, Sep 8, 2004
    #1
    1. Advertising

  2. John Black

    Moonlit Guest

    Hi,

    "John Black" <> wrote in message
    news:...
    > Hi,
    > In debugging a core dump, I run purify against my code, it says
    > several "Free Memory Read" errors. All the errors are on one delete
    > statement. Usually this means some pointer is deleted by non-owner. But
    > it is really hard to locate the error code.
    >
    > The code structure is like this:
    >
    > class BigClass{ // this is from somebody else code
    > ......
    > }
    >
    > // following is my code
    >
    > BigClass* big = new BigClass();
    >
    > ... operation on big ...
    >
    > delete big;


    It is a bit messy but what you could do is replace delete and free (in a
    header file so it is easy to add and remove) with a macro that prints the
    line number (__LINE__ and source file name (__FILE__) and the address after
    new and delete and if it is a memory allocation or free then execute the new
    or delete. This way you can track what memory is allocated where and
    deleted. It might or might not help you. If the code is a complete mess you
    probably have to rewrite it anyway :-(


    >
    > The core dump is on "delete big;", and purify says that it trying to
    > free 4 bytes at 0x123456 which passes 85 bytes of a freed block, and
    > that block is generated by func1().
    >
    > The problem is func1() is a very basic function which is called all
    > over the code, so I have no clue to know the high level constructor.
    >
    > Wondering if you have any suggestion in catch such kind of error.
    >
    > Thanks.
    >
    >


    Regards, Ron AF Greve
     
    Moonlit, Sep 8, 2004
    #2
    1. Advertising

  3. It sounds like you are deleting the object more than once.
    I've solved problems like this by putting some debug code
    in the object's destructor and use to debugger and wait for that particular
    free'd address (this pointer) to pop up. Often the program will allocate
    the memory in
    exactly the same sequence run-after-run so you can do this,
    otherwise you have to be more crafty.

    If you are using MFC on Windows, they have a few functions to
    check the heap, I think it was AfxCheckMemory(), this works
    quite efficiently, you can sprinkle these through the likely spots
    where you are having problems or at strategic points in your program
    to narrow down where your problem is happening. I seem to recall
    you can put breakpoints in the windows C run-time functions too and
    track memory management there too.


    Although purify is a wonderful tool, it points out the problem but often it
    takes a lot of sweat and effort to figure out the reason for it.



    "Moonlit" <news moonlit xs4all nl> wrote in message
    news:413f5677$0$566$4all.nl...
    > Hi,
    >
    > "John Black" <> wrote in message
    > news:...
    > > Hi,
    > > In debugging a core dump, I run purify against my code, it says
    > > several "Free Memory Read" errors. All the errors are on one delete
    > > statement. Usually this means some pointer is deleted by non-owner. But
    > > it is really hard to locate the error code.
    > >
    > > The code structure is like this:
    > >
    > > class BigClass{ // this is from somebody else code
    > > ......
    > > }
    > >
    > > // following is my code
    > >
    > > BigClass* big = new BigClass();
    > >
    > > ... operation on big ...
    > >
    > > delete big;

    >
    > It is a bit messy but what you could do is replace delete and free (in a
    > header file so it is easy to add and remove) with a macro that prints the
    > line number (__LINE__ and source file name (__FILE__) and the address

    after
    > new and delete and if it is a memory allocation or free then execute the

    new
    > or delete. This way you can track what memory is allocated where and
    > deleted. It might or might not help you. If the code is a complete mess

    you
    > probably have to rewrite it anyway :-(
    >
    >
    > >
    > > The core dump is on "delete big;", and purify says that it trying to
    > > free 4 bytes at 0x123456 which passes 85 bytes of a freed block, and
    > > that block is generated by func1().
    > >
    > > The problem is func1() is a very basic function which is called all
    > > over the code, so I have no clue to know the high level constructor.
    > >
    > > Wondering if you have any suggestion in catch such kind of error.
    > >
    > > Thanks.
    > >
    > >

    >
    > Regards, Ron AF Greve
    >
    >
     
    Dave Townsend, Sep 9, 2004
    #3
  4. John Black <> wrote in message news:<>...
    > Hi,
    > In debugging a core dump, I run purify against my code, it says
    > several "Free Memory Read" errors. All the errors are on one delete
    > statement. Usually this means some pointer is deleted by non-owner. But
    > it is really hard to locate the error code.
    >
    > The code structure is like this:
    >
    > class BigClass{ // this is from somebody else code
    > ......
    > }
    >
    > // following is my code
    >
    > BigClass* big = new BigClass();
    >
    > ... operation on big ...
    >
    > delete big;
    >
    > The core dump is on "delete big;", and purify says that it trying to
    > free 4 bytes at 0x123456 which passes 85 bytes of a freed block, and
    > that block is generated by func1().
    >
    > The problem is func1() is a very basic function which is called all
    > over the code, so I have no clue to know the high level constructor.
    >
    > Wondering if you have any suggestion in catch such kind of error.
    >
    > Thanks.


    John,

    What platform are you running on? There are many heap debugging tools
    available. Purify is an excellent tool but sometimes it does not give
    enough information. You can check out mpatrol which is widely ported.
    You can also try Valgrind if you are on Linux. My company, Dynamic
    Memory Solutions, markets Dynamic Leak Check for leak checking and
    memory debugging. It runs on Solaris and Linux.

    Can you post some more of the code? Do you have the dtor source code
    for class big? It may be an error in the dtor. I have a bit of
    expertise in this area and would be happy to help.

    Matthew
    Dynamic Memory Solutions
    www.dynamic-memory.com
     
    Matthew Fisher, Sep 10, 2004
    #4
    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. Subodh
    Replies:
    0
    Views:
    816
    Subodh
    Jan 7, 2004
  2. chanmm
    Replies:
    2
    Views:
    10,764
    brinda
    Sep 7, 2010
  3. avishosh
    Replies:
    2
    Views:
    10,700
    avishosh
    Aug 8, 2004
  4. Replies:
    6
    Views:
    937
    Sherm Pendley
    May 19, 2005
  5. Lee Stone
    Replies:
    2
    Views:
    158
    Lee Stone
    Sep 11, 2008
Loading...

Share This Page