Find memory leaks ?

Jul 6, 2013 at 8:35am

Find memory leaks ?

Hi folks,

As an exercise, i try to detect the leaks created in the code below.

class_new("jojo", (method)jojo_new, NULL, sizeof(t_jojo), NULL, A_GIMME, 0);

void *jojo_new(t_symbol *s, long argc, t_atom *argv)
    t_jojo *x = (t_jojo *)object_alloc(jojo_class);
    x->n = (long *)malloc(100000 * sizeof(long));
    return x;

There’s no free method and consequently it never releases the malloced block at destroy time.

I tried :

  1. Using “Xcode” with “Instruments”. I add Max Runtime in Executables. Run with Performance Tool / Leaks. I open / close the test patch. No result (only JUCE noise).
  2. Using command-line tool “leaks”. (leaks -p PID -nocontext). Nothing better.
  3. Using “MallocDebug”. Crashed Max Runtime.

What is your strategy to find memory leaks in your external ?
Is there any option i need to select ?
Is there something i did wrong ?
Is there any secret tool ?


(Xcode 3.2 / Mac OS X 10.6.8 / MaxMSP 5.1.9)

Jul 7, 2013 at 1:00am


There’s a limited chance that any of the tools you mention will ever reveal anything, because of

  • the copy protection
  • the memory pool, which doesn’t always free the memory right away but keep it for later use

Your best chance is discipline ;-) balance your alloc/dealloc calls and you’ll be fine. How cool is that ;-)


Jul 7, 2013 at 1:20am



- Copy protection. I expected to Max Runtime to be friendly. Nope. Good to know.
- Memory pool. I suspected something like that. An esoteric line (that i probably misunderstand) in the report indicates that the block malloced in my object still gets a ref count of 1 even after have been destroyed.
- In a world where discipline is sufficient, in this world there is no crash.
- But there are some !

Ok. I’ll implement a global counter ; and that’s not cool ;-)

Jul 17, 2013 at 7:59am

Write test cases for you code, run it through (whatever the c version of cppunit/junit is) and then use memory analyser on your test runs… at least you know your code is good them, whatever mechanisms are used in the MAX world


You must be logged in to reply to this topic.