Memory leaks when interfacing with Octave


I have been trying to create a dynamic extension to Octave which uses ROOT to read a TTree that represents a series of coincidence events detected in a PET scanner. I will then use Octave’s convenient language to interface with my own C++ reconstruction routines.

While everything was really simple to code, I was getting a strange memory leak. None of my extensions that did not make use of ROOT prsented the problem. Eventually, I found it could be reproduced even with the simplest Octave extension compiled against ROOT one could imagine:

#include <octave/oct.h>
#include <root/TFile.h>

DEFUN_DLD( memtest, args, , “Tests for memory leak.” )
return( octave_value() );

The code above, compiled with

mkoctfile $(root-config --libs --noauxlibs | sed s/-pthread// | sed s/-rdynamic//) -o memtest.oct

generates a loadable function file memtest.oct, which can be called from Octave if in the path. Repeated calls to this function cause memory usage to grow steadly, but with no apparent regularity in growth size. Both ROOT and Octave are the default ones provided by Ubuntu.

What should I do to better diagnose and solve the problem? Any help will be appreciated.

Thanks in advance,

I am replying to my own post to add a few things I have found out which make me think the bug is more on the Octave side.

If I do not clear Octave’s global function table with “clear all” there is no memory increase for each call to “memtest”. It seems that the interpreter is reloading unnecessary stuff. Even more likely, I guess it is simply not being able to unload what is no longer needed, but still has to reload a lot of stuff when a call for memtest is done again.

So, sorry for bothering you with potentially non-ROOT issues.