How to call mxArray with varargin in C; mxCreateCellMatrix and mxSetCell?

1 view (last 30 days)
Compile/build works, but I only get the first argument into matlab function and then program (.exe) crashes. Can I use the mxCreateCellMatrix as input to varargin?
int wmlfNor(char* filenameinp, char* filenameana, char* filenamecsv){
int nargout=1;
mxArray *pfinp;
mxArray *pfana;
mxArray *pfcsv;
mxArray *pvarargin;
mxArray *out=NULL;
int returnval=isMCRrunning();
if(!returnval)
return returnval;
pfinp=mxCreateString(filenameinp);
pfana=mxCreateString(filenameana);
pfcsv=mxCreateString(filenamecsv);
pvarargin=mxCreateCellMatrix(3,1);
mxSetCell(pvarargin,0,pfinp);
mxSetCell(pvarargin,1,pfana);
mxSetCell(pvarargin,2,pfcsv);
%Call to my Matlab function
returnval=mlfNor(nargout,&out, pvarargin);
mxDestroyArray(pfinp);
mxDestroyArray(pfana);
mxDestroyArray(pfcsv);
mxDestroyArray(pvarargin);
mxDestroyArray(out);
return returnval;
}

Accepted Answer

Geoff Hayes
Geoff Hayes on 17 Oct 2014
Oyvind- it isn't clear how you are executing the above code. Is it part of a C program that you are running? Where is the function that you are providing the variable number of inputs to? How does wmlfNor relate to this function, as you seem to be just passing in strings here?
If I take the body of your above code and paste it into a MEX function (commenting out the returnval stuff and the call to the mlfNor function), the build/compile works fine, but crashes as soon as I call the MEX function. (Note that I created dummy strings in place of your input parameters.)
What seems to be happening is that the code is trying to destroy/free the same allocated memory twice at
mxDestroyArray(pfinp);
mxDestroyArray(pfana);
mxDestroyArray(pfcsv);
mxDestroyArray(pvarargin);
We destroy the mxArrays that correspond to the three strings created with mxCreateString, and then try to destroy the cell array, pvarargin, which tries to destroy/free all cells...which includes the three (string) mxArrays which we just destroyed. If you comment out these three lines and just do
//mxDestroyArray(pfinp);
//mxDestroyArray(pfana);
//mxDestroyArray(pfcsv);
mxDestroyArray(pvarargin);
it works fine: there is no crash when running the MEX function. To see the effects of this more clearly, do the following: free/destroy the memory to the three string arrays, and then set each of the three cells in pvarargin to NULL, then destroy the cell array
mxDestroyArray(pfinp);
mxDestroyArray(pfana);
mxDestroyArray(pfcsv);
mxSetCell(pvarargin,0,NULL);
mxSetCell(pvarargin,1,NULL);
mxSetCell(pvarargin,2,NULL);
mxDestroyArray(pvarargin);
Again, the function can be built/compiled without any problems, and the MEX function can be run.
Try using one of the above two options in your code, and see if that prevents the crash.
  1 Comment
Oyvind
Oyvind on 20 Oct 2014
It works, thank you. I had another error in the code, should be
pvarargin=mxCreateCellMatrix(1,3);
The code is a layer/library (.dll) between C-code the test engineers write (.exe) and my matlab function mlfNor.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Compiler 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!