By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,649 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

Memory Leak

P: n/a
GJ
Hi,

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

--
GJ
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

Nov 15 '05 #2

P: n/a
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...

Nov 15 '05 #3

P: n/a
makuchaku <ma********@gmail.com> 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.
Nov 15 '05 #4

P: n/a
On 2005-10-26, Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
makuchaku <ma********@gmail.com> 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
Nov 15 '05 #5

P: n/a
On Wed, 26 Oct 2005 11:28:05 +0000, Jordan Abel wrote:
On 2005-10-26, Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
makuchaku <ma********@gmail.com> 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
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.