Issue with Mex Function
Show older comments
I tried running a very simple example with a mex file and a structure with another structure inside of it and ran into an issue. I created a standalone C++ file with this code:
#include <stdio.h>
#include "mex.h"
void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray
*prhs[])
{
mxSize dims[2] = {1, 1};
const char* fields1[] = {"field1", "field2"};
const char* fields2[] = {"field3"};
mxArray *mexMsg = mxCreateStructMatrix(1, 1, 2, fields1);
mxArray *subMexMsg = mxCreateStructMatrix(1, 2, 1, fields2);
int intValue = 32;
mxArray* inputMxArray = mxCreateDoubleScalar((double) intValue);
mxSetField(subMexMsg, 0, "field3", inputMxArray);
mxSetField(mexMsg, 0, "field1", subMexMsg);
mxSetField(mexMsg, 0, "field2", inputMxArray);
plhs[0] = mexMsg;
}
My goal was to create a structure of structures. The result (let’s call it res) should have looked like this res field1 subMessage field3 32 field2 32
I compiled it with Matlab using mex and ran it. Everything looked fine and as expected. Then I executed ‘clear all’ and it failed with a segfault. I hope that this should be easily repeatable on another machine. I expect that the error came about when trying to clear the mxArrays that were created in the mex function. Anyone know why this error is occurring? Thank you for any help.
Accepted Answer
More Answers (0)
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!