Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to locate memory error

Reply
Thread Tools

how to locate memory error

 
 
John Black
Guest
Posts: n/a
 
      09-08-2004
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.


 
Reply With Quote
 
 
 
 
Moonlit
Guest
Posts: n/a
 
      09-08-2004
Hi,

"John Black" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Dave Townsend
Guest
Posts: n/a
 
      09-09-2004

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$(E-Mail Removed)4all.nl...
> Hi,
>
> "John Black" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
>
>



 
Reply With Quote
 
Matthew Fisher
Guest
Posts: n/a
 
      09-10-2004
John Black <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
perl upgrade - make test error "Can't locate Config_heavy.pl" Lee Stone Perl Misc 2 09-11-2008 03:16 PM
Net::SMTP error: Can't locate Mail/Sender.pm in @INC 50295@web.de Perl Misc 6 05-19-2005 08:12 PM
The system cannot locate the resource specified. Error processing resource avishosh XML 2 08-08-2004 06:28 AM
HTTP::Response error : "Can' t locate loadable object" Subodh Perl 0 01-07-2004 12:11 PM



Advertisments