Strange mex behavior - calling second function corrupts inputs in MatLab?

1 view (last 30 days)
I am having some very strange mex behavior. I have two mex functions, both of which generate 2 arrays of integer outputs. The outputs from one can be used as the inputs to another. When this is done, the output from the first function (viewed in matlab) becomes corrupted (takes different, non-integer values). Additionally, calling the second function seems to cause matlab to become unstable - closing matlab after running it causes matlab to crash, as does trying to run it twice within one session. No errors are generated, just a crash. All this together makes me suspect some sort of memory issue, but I have no idea what could cause it. Any ideas?
Sketch of what happens:
MxFn1(inputs)
read inputs in with mxGetPr
Get two integer arrays A,B
Convert to double arrays, output with mex
Back in Matlab, at this point A,B read correctly
call MxFn2(inputs, including A,B)
A, B read in correctly (verified with mexprintf)
A,B themselves are not changed, just copied into a new array
MxFn2 runs
MxFn2 creates A'B', converts to double arrays, outputs
A',B' are correct (verified with mexprintf)
output A',B'
A,B,A',B'now ready incorrectly in MatLab
Attempting to run MxFn2 again causes a crash, as does exiting matlab
I use the same input/output methodology in both functions.
  4 Comments
Geoff Hayes
Geoff Hayes on 4 Apr 2015
Ryan - could you show (from the Command Window) how you are calling your functions, checking the input and output, and noting how the A',B' are then returned as outputs to matlab, where they are no longer correct?
Ryan Pyle
Ryan Pyle on 7 Apr 2015
Found my mistake - I had accidentally called mxFree on Wr, I0r where Wr and I0r where my plhs[0] and plhs[1] in the second function. Thanks for your help!

Sign in to comment.

Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!