segfaults from persistent data in mex file

I have a mex file where compute time is critical, which gets called many times during execution. In order to make things fast, it caches a rather large object that gets updated periodically. I don't want to make copies of this object every time I return to matlab, and I also can't allocate it directly in matlab because the updates (And memory allocations) can happen in parallel. Hence, I use a global variable to keep track of this cache (ugly, I know, but it works). I'm following advice from this thread.
However, very rarely I get segfaults from this approach. If I turn the cache off I get no segfaults, and the cache is straightforward enough that I'm reasonably sure my code isn't at fault. Often these segfaults print no stacktrace (and when they do, it tends to mention a 'free' operation but not much else of use). They happen maybe once every 100,000 executions of the mex file. Even stranger, it seems like updating the mex function and recompiling will cause matlab to segfault without even executing the mex file--i.e. I just resume matlab from a suspended state and it segfaults immediately.
Hence, I suspect something is getting messed up when matlab clears the file from memory. I haven't written a destructor for the cache, but I would have expected the worst outcome to be a simple memory leak and not a segfault. So my question is, what exactly happens when matlab clears a mex file from memory? The cache is simply allocated with the code:
struct CacheCell{
int xmin;
int ymin;
int xmax;
int ymax;
double* data;
CacheCell():xmin(0),ymin(0),xmax(0),ymax(0),data(NULL){}
};
vector<vector<CacheCell> > cache;
which is outside of the mexFunction. Code in the mexFunction will fill up the vectors, and allocates/deallocates the 'data' field using 'new' and 'delete'.
Edit: Seriously, no replies? Somebody at the mathworks must know how this works...

Answers (0)

Categories

Asked:

on 20 Feb 2014

Edited:

on 28 Feb 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!