[comp.lang.c] balancing free

rl@cbnewsl.att.com (roger.h.levy) (06/23/91)

Is there a reasonable way to determine if all malloc's in a program
are balanced by free's?  Since free() doesn't have a return value,
it may not be clear in complex pointer manipulation situations that
it's even been passed a pointer that points at dynamic memory.  I was
a little encouraged to see a function called coreleft() in Turbo C.
I hoped that by sprinking my program with this function, I could
audit the use of dynamic memory, but after some experimentation, I'm
not certain what this function really reports.  It may be reporting
the largest available block but that prevents me from accurately seeing
available memory shrink and grow.  My need is to verify that an
embedded systems controller won't slowly leak memory over a long
haul but I can do unit testing in a UNIX or PC environment.

Roger Levy
rl@groucho.att.com

rags@hpcuhc.cup.hp.com (Balakrishna Raghunath) (06/23/91)

"memory leaks" has recieved much attention in the press. I just happen to have
one of the journals lying on  my desk. Dr. Dobb's journal August 1990 
(special issue on C programming).  An interesting and easy to implement 
method for detecting memory leak is discussed.  

rags

worley@compass.com (Dale Worley) (06/24/91)

To prevent memory leaks and detect invalid uses of free, I once wrote
wrappers for malloc and free that kept all allocated blocks on a
doubly-linked list.  At the end of processing, the list should be
empty.  Also, free can check that the block is properly linked in (the
blocks pointed to by the forward and backward pointers have to point
back to the original block).  You can also put guard words before and
after the section returned to the user to make sure he isn't writing
off the end of the block.  That code saved me a lot of time hunting
for bugs.

Dale Worley		Compass, Inc.			worley@compass.com
--
If you can't drink a lobbyist's whisky, take his money, sleep with his
women and still vote against him in the morning, you don't belong in
politics.	-- Speaker of the California Assembly Jesse Unruh

unicorn@uxh.cso.uiuc.edu (Harry E Miller) (06/24/91)

rags@hpcuhc.cup.hp.com (Balakrishna Raghunath) writes:

>"memory leaks" has recieved much attention in the press. I just happen to have
>one of the journals lying on  my desk. Dr. Dobb's journal August 1990 
>(special issue on C programming).  An interesting and easy to implement 
>method for detecting memory leak is discussed.  

This is a very usefull article, the code that comes with it is very, very
usefull.  Somewhere buried under all the junk on my desk are two more
DDJ articles (August 1989 & May 1990) that have memory management routines
that look usefull, but I have not implemented them as of yet.

Harry E. Miller - "The Nine-Fingered Bandit"
unicorn@uxh.cso.uiuc.edu