Clear All and opensim mex functions
Posted: Wed Mar 25, 2015 3:03 pm
Hey everyone!
I am trying to create a mex file to call the opensim api in parallel using openMP. The mex file I created works great and gives the expected outputs until I use a "clear all" in the matlab workspace. After that, when I call the mex function I get a segmentation fault every time. I've narrowed down the location of the seg fault to a line where I call the Model command at the beginning of the code. My code is lengthy, but I've included the initial section where the crash is occurring.
Has anyone experienced this before?
PS Using mexLock prevents the mex file memory from clearing and resolves the issue, but puts me at greater risk of having a memory leak. I'd prefer not to use it.
static Model *osimModel[NTHREADS];
static State *osimState[NTHREADS];
static bool modelIsLoaded = false;
void ClearMemory(void)
{
for (int i=0; i<NTHREADS; ++i)
delete osimModel;
mexPrintf("Cleared memory from opensimPointKin mex file.\n");
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (nrhs == 1) // Load model if only one right hand side argument
{
if (modelIsLoaded == true)
ClearMemory();
// extract filenames
string model_name = mxArrayToString(prhs[0]);
for (int i=0; i<NTHREADS; ++i)
{
// Initialize model
osimModel = new Model(model_name); // name of Opensim model to load
// Initialize state
osimState = &osimModel->initSystem();
}
modelIsLoaded = true;
mexAtExit(ClearMemory); // exit function to clear memory allocated in heap
}
}
I am trying to create a mex file to call the opensim api in parallel using openMP. The mex file I created works great and gives the expected outputs until I use a "clear all" in the matlab workspace. After that, when I call the mex function I get a segmentation fault every time. I've narrowed down the location of the seg fault to a line where I call the Model command at the beginning of the code. My code is lengthy, but I've included the initial section where the crash is occurring.
Has anyone experienced this before?
PS Using mexLock prevents the mex file memory from clearing and resolves the issue, but puts me at greater risk of having a memory leak. I'd prefer not to use it.
static Model *osimModel[NTHREADS];
static State *osimState[NTHREADS];
static bool modelIsLoaded = false;
void ClearMemory(void)
{
for (int i=0; i<NTHREADS; ++i)
delete osimModel;
mexPrintf("Cleared memory from opensimPointKin mex file.\n");
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (nrhs == 1) // Load model if only one right hand side argument
{
if (modelIsLoaded == true)
ClearMemory();
// extract filenames
string model_name = mxArrayToString(prhs[0]);
for (int i=0; i<NTHREADS; ++i)
{
// Initialize model
osimModel = new Model(model_name); // name of Opensim model to load
// Initialize state
osimState = &osimModel->initSystem();
}
modelIsLoaded = true;
mexAtExit(ClearMemory); // exit function to clear memory allocated in heap
}
}