how to locate memory error

J

John Black

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.
 
M

Moonlit

Hi,

John Black said:
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
 
D

Dave Townsend

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.
 
M

Matthew Fisher

John Black said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

strstream Memory Leak 5
Memory error 3
Custom Minecraft launcher client error; I think regarding java 0
Why memory leaks? 4
Possible memory leak? 11
Memory leaking 6
memory leak 4
Where to free memory ? 1

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top