Memory Leak

G

GJ

Hi,

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

Antonio Contreras

GJ said:
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.
 
M

makuchaku

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

Christopher Benson-Manica

makuchaku said:
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?
 
J

Jordan Abel

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
 
N

Netocrat

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top