john
john

Reputation: 1393

How to avoid memory leak during development in c

Iam programming in C. I want to know what practice we should follow to avoid Memory Leaks at the time of development itself. Please mention the precautions to be taken specially while dealing with strings and dynamic memory allocation.

Upvotes: 8

Views: 5661

Answers (5)

There is possibility of garbage collecting in C, in particular by using Boehm's conservative GC. To use it, replace malloc by GC_malloc, strdup by GC_strdup in all your program and you should not bother calling free or GC_free. Boehm's GC work verys well in practice (even if in theory there is a small probability of leakage).

Notice that being a live data is not a modular property: a given data chunk is alive in a whole program (not in a particular given module).

To answer the original question, an important issue is to define an allocation policy, and to document it. In particular, every function returning dynamically allocated data should state how, and by whom, should that data be free-d.

addenda

A useful tool to hunt memory leakage bugs in C (or C++) on Linux is valgrind. Don't forget to pass the -g -Wallflags to gcc and g++ when developing your code.

Upvotes: 3

Employed Russian
Employed Russian

Reputation: 213935

I don't agree with down-votes on this question. I think it's a real question, and quite deep.

On the surface, the answer is "call free on any memory you malloced".

But the real answer is that your design should include a clear ownership model. The only way to avoid memory leaks and access to dangling memory is to always know, for every piece of dynamically allocated memory, what object owns that memory (and is responsible for disposing of it).

If you don't have such a clear ownership model, you will forever hunt memory leaks and use-after-free bugs (this also applies to C++). Use of garbage collector would allow you to plaster over these problems, at the cost of significant CPU cycles.

If you do have a clear ownership model, these problems generally just disappear: the owner frees all memory it owns when it itself is disposed of.

Upvotes: 14

another.anon.coward
another.anon.coward

Reputation: 11405

Use variables on stack if possible rather than using memory from heap.

Try to avoid common mistakes, a few pointers:

  1. Make sure to call free() when you use malloc() or calloc().
  2. Don't reassign pointer which points to allocated memory location without free()ing it first i.e. don't lose the reference.
  3. Be careful when using realloc(). Do not use the same pointer for input & output parameters.

Avoid common mistakes made using strings, a few pointers:

  1. Make sure there is memory for terminating NUL character.
  2. Make sure string is NUL terminated in all your use cases (even when used in functions like strncpy() etc.)

Learn to use a debugger (gdb)
Learn to use static analysis tools. Tools like splint, valgrind, clang can be installed on you linux system from your distro's package repository.

Few useful links:
c-faq - Arrays & Pointers
c-faq - Memory allocation
Secure C Coding - Memory Management
SO Question related to avoiding memory leak in C/C++
yolinux tutorial

Hope this helps!

Upvotes: 9

M S
M S

Reputation: 4093

The only way to avoid memory leak is to manually free() all the memory allocated by you in the during the lifetime of your code.

You can use tools such as valgrind to check for memory leaks. It will show all the memory that are not freed on termination of the program.

Upvotes: 3

griegs
griegs

Reputation: 22770

I'm no C programmer but generally you need to destroy or dispose of everything you are no longer using.

If you do not dispose of objects, and string are objects, then the memory may not be garbage collected and may stay resident in memory.

Upvotes: 0

Related Questions