memory leak?

Q

questions?

I am new to C programming.
I just wrote a quite long program for my project, but I have memory
leak and the program keeps asking for more and more memory.

What are the basic rules for checking potential places for memory leak?
Are they any program to check which line is asking for a lot of
memeory?

Thanks a lot
 
M

Mark McIntyre

What are the basic rules for checking potential places for memory leak?

Everything that is *alloc'ed must be free'd. There's not much more to
it than that.
Are they any program to check which line is asking for a lot of
memeory?

Sure, several. A websearch may turn them up.
Mark McIntyre
 
D

David Resnick

questions? said:
I am new to C programming.
I just wrote a quite long program for my project, but I have memory
leak and the program keeps asking for more and more memory.

What are the basic rules for checking potential places for memory leak?
Are they any program to check which line is asking for a lot of
memeory?

Thanks a lot

There are tools such as valgrind (open source) and purify ($$$) that
may or may not be available for your platform. I like both.
If you are interested in tools, asking in a group dedicated to
your platform of interest is the right thing to do.

A portable alternative is to have wrappers around malloc/calloc/realloc
and free and log allocations/frees and the line of code that is doing
them. Those logs plus some post-processing can help sort things out.
This only works if the allocations being leaked go through these
wrappers (and are not, say, in some libraries not under your control),
but it can be a decent approach.

-David
 
A

August Karlstrom

questions? said:
I am new to C programming.
I just wrote a quite long program for my project, but I have memory
leak and the program keeps asking for more and more memory.

What are the basic rules for checking potential places for memory leak?
Are they any program to check which line is asking for a lot of
memeory?

If your memory allocation strategy is complicated and you are willing to
sacrifice some speed (but gain reduced complexity) you can use a
garbage collector, e.g. Hans Boehm's:

http://www.hpl.hp.co.uk/personal/Hans_Boehm/gc/


August
 
B

Ben C

You can use valgrind with its memcheck tool on some systems.

http://valgrind.org/

There are other tools like this.

Or just write little wrappers for malloc and free that keep track of
everything. __FILE__ and __LINE__ can be useful for this-- your memory
tracker can keep track of where in the source the calls came from.

Something like this:

void *my_malloc(size_t size, const char *file, unsigned line)
{
void *ret = malloc(size);
static unsigned count = 0;

/* store ret in a list somewhere perhaps */

printf("%uth allocation of %p from %s:%u\n", count, ret, file, line);
count++;
return ret;
}

void my_free(void *p)
{
/* remove p from the list */

printf("Freeing %p\n", p);
free(p);
}

Then in something that's included by all source files:

extern void *my_malloc(size_t, const char *, unsigned);
extern void my_free(void *);

#define malloc(size) my_malloc(size, __FILE__, __LINE__)
#define free my_free

At the end of the program, anything left in the list is leaked.
Alternatively don't bother with a list, and just parse the output with a
scripting language-- you've got a record of all the allocations and
deallocations there. Do whichever's easier.

You can tell from what got printed out which allocations leaked. If you
run the program again (provided it doesn't depend on any external input
that might change), you can use a debugger to investigate the leaked
allocations as they are made.

Another way to do this is to use a debugger like gdb as a tracer; you
can set commands on a breakpoint to print a backtrace and then continue.
This might help you find the leaking backtraces. But you might not need
to resort to this.
 

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


Members online

No members online now.

Forum statistics

Threads
473,795
Messages
2,569,642
Members
45,356
Latest member
deepthi.kodakandla

Latest Threads

Top