4

I am trying to complete a college assignment, and the marking criteria specifies 5% for memory management - specifically for having no memory leaks.

As I understand it, memory leaks in simple C programs, are only caused by pointers which have become abandoned by the program - ie, malloc/calloc/etc calls which are never have a corresponding free.

My question is in 3 parts:

  1. Whats the simplest way on Solaris and OSX to 'prove' that you haven't leaked any memory?
  2. Does XCode have any tools to help determine memory leaks?
  3. Does the operating system release all previously allocated memory within a c program once the process ends?
Ash
  • 24,276
  • 34
  • 107
  • 152
  • 2
    Nr. 1 is more complex then it sounds. It also means you need an fclose for your fopen and destroyFoo() for your createFoo(). – Mel May 17 '11 at 22:30
  • Not an answer to your question, but worth mentioning: your instructor's idea of memleaks may be different from what matters in the real world. A bounded number of unfreed allocations (independent of any data the program processes) are not a real memory leak despite being caught by tools like valgrint, whereas data-dependent allocations that persist after the data is no longer being used, and get cleaned up only on program exit, *are* real leaks despite the fact that no tool can detect them. – R.. GitHub STOP HELPING ICE May 17 '11 at 23:01
  • You can try deleaker but it only for windows( – MastAvalons Feb 06 '12 at 21:21

4 Answers4

5

Valgrind is your friend.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
3
  1. For every malloc(), you need to ensure that you have exactly one free().
  2. I haven't worked with XCode, but this forum entry may help.
  3. Yes. It's still poor form to let your running program 'leak,' however.

In general, it's a good idea to learn how to avoid leaks without using tools like a memory debugger (early on) -- especially for your simple programs. It's painful, however: when it comes to building anything non-trivial you'll want to start learning how to use the more advanced debugging tools (like Valgrind, as Alex Reynolds suggested in another answer.)

Joe
  • 800
  • 4
  • 15
  • When referring to other answers, never say "above" or "below", because the order of answers is not fixed. Just say something like "Alex's answer". – cjm May 17 '11 at 22:59
2

Answer for Mac OS and an example to be avoided (saved you half an hour).


Mac OS doesn't come with Valgrind or dmalloc. Moreover, Valgrind has some compatibility issues when trying to get it installed in Sierra.

There is utility called "leaks", which I get it running by this:

leaks -atExit --/Contents/Developer/usr/lib/libLeaksAtExit.dylib ./a.out

Unfortunately, this doesn't report obvious memory leaks... Maybe I am using it wrong, but I was just searching for an easy way to check that my C program free'd its memory as it should.

If you have time, then maybe read and use Using OSX Leaks for C Programs on the Command Line?

Resources:

  1. Finding Memory Leaks
  2. Using the "leaks" command on a C/C++ executable

PS: Maybe if used with "iprofiler", then it might be useful, but I didn't had it installed.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

there's dmalloc too

freethinker
  • 1,286
  • 1
  • 10
  • 17