mexCallMatlab failing to return

8 views (last 30 days)
Pat
Pat on 28 Jan 2015
Answered: Pat on 29 Jan 2015
I'm using C to process a stream of images from a file, this C function is called from an M-file. The image stream is being parsed using a separate Matlab function, so in my C loop for all images I use mexCallMatlab to call that Matlab function to parse the image file structure. For the most part this appears to work fine, but for some image streams it crashes (it appears to only happen with longer streams but I can't swear to that as it's currently happening at image 327 and 630 for two different files so not consistent). The image stream is proprietary, but basically an XML wrapper around raw image data.
The weird thing is it does not throw an error it just hangs and sits there indefinitely. Matlab doesn't appear to be using any more processing power or memory than usual and everything runs fine. The only way to get out of it (that I know of) is Ctrl-q which kills Matlab, Ctrl-c does nothing.
Since I don't have a good way to debug the C I'm using print statements. The last line of the Matlab function is:
disp(frame)
and the next line in the C file is
mexPrintf("Image %d returned\n",i);
After some number of iterations through the loop I see the frame number get printed but I never see the "Image XX returned", the cursor just sits there and does nothing, I do not get returned to the command prompt.
Unfortunately I can't share much of my code but here are what I believe are the important excerpts. I played with the memory management a lot as I figured it was something like that causing the problem, but I'm not so sure since it's inconsistent, here's what I've settled on anyway:
mxArray* file_info; mxArray* return_array[2];
...
file_info = mxDuplicateArray(prhs[0]);
...
mexCallMATLAB(2, return_array, 1, &file_info, "getCurrentFrame"); mxDestroyArray(file_info); file_info = mxDuplicateArray(return_array[1]); //Copy the updated file info structure
...
For All Images {
mexPrintf("Processing image %d of %d - ",i,N_images);
mexCallMATLAB(2, return_array, 1, &file_info, "getCurrentFrame");
mexPrintf("Image %d returned\n",i);
mxDestroyArray(file_info);
file_info = mxDuplicateArray(return_array[1]); //Copy the updated file info structure
<Processing>
}
Here's what I see on the console (note there's an intentional offset in the image numbering between the Matlab and C):
...
Processing image 624 of 1460 - 628
Image 624 returned
Processing image 625 of 1460 - 629
Image 625 returned
Processing image 626 of 1460 - 630

Accepted Answer

Pat
Pat on 29 Jan 2015
It was actually a bug further on in the code causing an infinite loop (char counter trying to go over 255). Not sure why the mexPrintfs weren't being executed even though it had passed them by.
Also apparently Ctrl-c can't kill mex files while they're being executed.

More Answers (0)

Categories

Find more on Read, Write, and Modify Image 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!