Matlab shuts down after executing a mex call

Hi,
I'm calling a C++ function using Mex. The function call works fine. After the file is called and the result is displayed, Matlab shuts down by itself. How do I stop this from happening?

 Accepted Answer

plhs[] is an array that MATLAB creates when you call the mex function to hold the return variables. But MATLAB doesn't create these variables for you, it simply creates a pointer array to hold the pointers to the return variables. You the programmer are expected to create these variables. When you enter the mex function, plhs[] values are not yet pointing to any variables. When you do this:
x = mxGetPr(plhs[0]);
You are asking mxGetPr to get the real data pointer from the variable pointed to by plhs[0], but plhs[0] is an uninitialized pointer, hence the crash. You need to do this:
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); // create the return variable
x = mxGetPr(plhs[0]);
And, as Walter points out, even if you do this as you currently have the code you will get a 0 returned value since you don't assign anything to the output yet via the x pointer. E.g., you could change the last line to this:
*x = test(a,b);

2 Comments

In addition, nrhs is read only. You can check it to see how many values are expected from the caller. But you cannot set it inside the Mex function.
Thanks James.
That clears it up.

Sign in to comment.

More Answers (2)

You probably have some memory corruption in the C++ function, with the effect of the corruption being observed when MATLAB goes to clear the variables that are no longer being used.

4 Comments

I'm testing mex functionality to see if it will work for something else I need.
I'm just passing 2 variables from matlab to the C++ function which adds them up and prints them.
Try posting code. James is pretty good at spotting mex problems if he has code to work with.
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <complex>
using namespace std;
#include "mex.h"
int test(int a, int b)
{
int s;
s=a+b;
cout<<s;
return s;
}
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int a, b, s;
double *x;
nrhs=2;
a=mxGetScalar(prhs[0]);
b=mxGetScalar(prhs[1]);
x=mxGetPr(plhs[0]);
test(a,b);
}
I call this by entering the following lines:
mex test.cpp;
a=1;
b=2;
sum=test(a,b)

Sign in to comment.

What do you do with the value you return from test() at the C level ? You are not somehow putting it in the the output location x (plhs[0]) in your mexFunction routine .

2 Comments

I'm not doing anything with it.
test() could be a function that returns void.
True that test() at your C level could return void, but at the MATLAB level you have indicated that you are expecting a return value from the call to the MEX routine, and you never set the return value to anything.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!