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

2 Kommentare

Charlie
Charlie am 23 Dez. 2014
Sometimes instead of crashing Matlab will spit out the following:
Only M functions may eval a parfor or spmd statement.
See Parallel Computing Toolbox documentation about Transparency.
Error in testcase/callback (line 5)
counter = counter + 1;
Error in testcase (line 11)
testcase(@callback);
Which doesn't seem to make any sense, making me wonder if I'm corrupting the internal state somehow
Charlie
Charlie am 23 Dez. 2014
Not sure if this would mean something to anyone, but I've attached a sample stacktrace.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Titus Edelhofer
Titus Edelhofer am 23 Dez. 2014
Bearbeitet: Titus Edelhofer am 23 Dez. 2014

0 Stimmen

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 Kommentare

Charlie
Charlie am 23 Dez. 2014
Thanks for taking a look Titus. Yeah good catch on the stacktrace - my original testcase mex was actually testcase.cpp, and the .m file was testcasem.m. I used more sensible names in the question but forgot to fix that.
Anyway I'll try putting it in a .c file and see if that helps. Did you try running it multiple times while making slight modifications to the .m file? That is what triggers the crash for me.
Titus Edelhofer
Titus Edelhofer am 23 Dez. 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
Titus Edelhofer am 23 Dez. 2014
Hi Charlie,
I added a "mxDestroyArray(arrays);" to prevent memory leaking ...
Titus
Charlie
Charlie am 23 Dez. 2014
Yes I previously had a mxDestoryArray call, but removed it to see if it would help. The callback may choose to hold on a reference to the argument passed to it, and I thought it was the source of my crashes.
I tried copying your code into a .c file and compiling that (still with VS2010), but it again crashes on the second call in an other vanilla R2012b session. Are you running 64 bit also? I'm on win 7 fwiw. Not really sure why it works for you...
Titus Edelhofer
Titus Edelhofer am 23 Dez. 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
Charlie am 23 Dez. 2014
Ok will give that a go, thanks! Good to know there's nothing obvious that I'm missing at any rate. Been banging my head against this one for a while...

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

am 23 Dez. 2014

Kommentiert:

am 23 Dez. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by