There are three ways to avoid them -
1. don't create memory leak (avoid while writing code), use free function when done with the memory.
2. use some static tool like coverty to detect memory leak and fix it.
3. use some dynamic tool ( run time) to detect memory leak eg. valgrind and fix it.