Mex crash with callback
Show older comments
Background: I'm writing a mex that needs to work with a large amount of data in batches. In order to avoid having to wrap a c++ object in a handle object, I'm instead executing a Matlab callback for each iteration (essentially option 2 from Oliver's initial post here).
I've isolated my problem to a testcase (see below) that produces random segmentation faults. At first I thought it was related to an improper mxDestroyArray but now I'm not freeing anything! Rather the issue appears to be related to the repeated use of a handle to a nested function. I get similar crashes when using an anonymous function handle. If I make it scoped however, it seems to be ok, likewise if I store the handle in a global and use a regular function to trampoline.
I've also tried mxDuplicateArray-ing the callback before each use, in-case there was some overzealous memory re-claiming, but that didn't help.
Note that it usually works correctly on the first run - if I then edit say the string that gets displayed in the callback and re-run, it will then crash.
I'm using Matlab 2012b and VS2010.
testcasemex.cpp :
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
if (nrhs != 1 || !mxIsClass(prhs[0],"function_handle"))
mexErrMsgTxt("Expected function handle.");
for (int i = 0; i < 20; i++) {
mxArray *arrays = mxCreateCellMatrix(1, 10);
for (int j = 0; j < 10; j++)
mxSetCell(arrays, j, mxCreateNumericMatrix(1000, 1000, mxUINT32_CLASS, mxREAL));
mxArray *args[] = {(mxArray *)prhs[0], arrays};
mexCallMATLAB(0, nullptr, 2, args, "feval");
}
}
testcase.m :
function testcase
counter = 1;
function callback(array)
counter = counter + 1;
disp('In callback')
size(array)
pause(0.1)
end
testcasemex(@callback);
end
Answers (1)
Titus Edelhofer
on 23 Dec 2014
Edited: Titus Edelhofer
on 23 Dec 2014
Hi,
hmm, it works fine for me, both with the current version and with R2012b. Only observation I have: in your comment it's written
Error in testcase (line 11)
testcase(@callback);
But that should be
testcasemex(@callback);
Titus
PS: I used a "C" version of your mex function instead of yours, but that should not make a difference...?
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
int i, j;
mxArray *arrays, *args[2];
if (nrhs != 1 || !mxIsClass(prhs[0],"function_handle"))
mexErrMsgTxt("Expected function handle.");
for (i = 0; i < 20; i++) {
arrays = mxCreateCellMatrix(1, 10);
for (j = 0; j < 10; j++)
mxSetCell(arrays, j, mxCreateNumericMatrix(1000, 1000, mxUINT32_CLASS, mxREAL));
args[0] = prhs[0];
args[1] = arrays;
mexCallMATLAB(0, NULL, 2, args, "feval");
/* destroy arrays to prevent memory leaking */
mxDestroyArray(arrays);
}
}
6 Comments
Charlie
on 23 Dec 2014
Titus Edelhofer
on 23 Dec 2014
Yes, I did. I changed the string in the disp, I changed size(array) to size(array{1}). No success in crashing MATLAB ;-).
Titus Edelhofer
on 23 Dec 2014
Hi Charlie,
I added a "mxDestroyArray(arrays);" to prevent memory leaking ...
Titus
Charlie
on 23 Dec 2014
Titus Edelhofer
on 23 Dec 2014
Strange. Just changed compiler to VS2010 (had lcc before). Running 32 bit MATLAB R2012b and 64 Bit R2014b ... No crash or error message at all. You should contact MathWorks Technical support, if they can reproduce the problem.
Charlie
on 23 Dec 2014
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!