Mex crash with callback

12 ビュー (過去 30 日間)
Charlie
Charlie 2014 年 12 月 23 日
コメント済み: Charlie 2014 年 12 月 23 日
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 件のコメント
Charlie
Charlie 2014 年 12 月 23 日
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 2014 年 12 月 23 日
Not sure if this would mean something to anyone, but I've attached a sample stacktrace.

サインインしてコメントする。

回答 (1 件)

Titus Edelhofer
Titus Edelhofer 2014 年 12 月 23 日
編集済み: Titus Edelhofer 2014 年 12 月 23 日
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 件のコメント
Titus Edelhofer
Titus Edelhofer 2014 年 12 月 23 日
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 2014 年 12 月 23 日
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...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by