Matlab freezes after calling .mexa64.

Hello everybody!
I've started using MEX functions over 2 days ago, but I've encounter some problems that I cannot solve. In particular when calling in Matlab the following mex function - it just stuck after C++ code done (doesn't return control back to Matlab).
Hoping someone of you can help me. Thanks a lot in advance!
Here's the code:
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
if (nrhs != 1)
mexErrMsgTxt("One input argument required.");
if (nlhs > 1)
mexErrMsgTxt("Too many output arguments.");
/* Check data type of input argument. */
if (!(mxIsDouble(prhs[0])))
mexErrMsgTxt("Input array must be of type double.");
mxArray *inputMat, *outputMat;
const mwSize *dims;
double *ptrInput;
double *ptrOutput;
int dimx, dimy, numdims;
int i, j;
// inputs
inputMat = mxDuplicateArray(prhs[0]);
// dimensions
dims = mxGetDimensions(prhs[0]);
numdims = mxGetNumberOfDimensions(prhs[0]);
dimy = (int)dims[0];
dimx = (int)dims[1];
// outputs
plhs[0] = mxCreateDoubleMatrix(dimx, dimy, mxREAL);
outputMat = mxCreateDoubleMatrix(dimx, dimy, mxREAL);
// pointers
ptrInput = mxGetPr(inputMat);
ptrOutput = mxGetPr(outputMat);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ> );
ofstream raw("/home/user/Downloads/rawData_Mex.txt");
for( i = 0; i < dimx; i++ )
{
pcl::PointXYZ p;
p.x = ptrInput[ 0 + dimy * i ]; cout << p.x << ' ';
p.y = ptrInput[ 1 + dimy * i ]; cout << p.y << ' ';
p.z = ptrInput[ 2 + dimy * i ]; cout << p.z << endl;
cloud -> points.push_back(p);
raw << cloud -> points[i].x << ' '
<< cloud -> points[i].y << ' '
<< cloud -> points[i].z << endl;
}
raw.close();
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ> );
ofstream filtered("/home/user/Downloads/filteredData_Mex.txt");
pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
cout << "Outlier removal created" << endl;
sor.setInputCloud (cloud);
cout << "Setting cloud as Input" << endl;
sor.setMeanK (50);
cout << "Setting K = 50" << endl;
sor.setStddevMulThresh (1.0);
cout << "Setting StddevMulThresh = 1" << endl;
sor.filter (*cloud_filtered);
cout << "Sending filtered data to cloud" << endl;
cloud_filtered = cloud;
for ( i = 0; i < cloud_filtered->points.size(); i++)
{
ptrOutput[ 0 + dimy * i ] = cloud_filtered->points[i].x;
ptrOutput[ 0 + dimy * i ] = cloud_filtered->points[i].y;
ptrOutput[ 0 + dimy * i ] = cloud_filtered->points[i].z;
filtered << cloud_filtered -> points[i].x << ' '
<< cloud_filtered -> points[i].y << ' '
<< cloud_filtered -> points[i].z << endl;
cout << "Adding filtered cloud" << endl;
}
filtered.close();
return;
}

2 Comments

What are the dimensions of prhs[0]? I.e., what are the values of dimx and dimy?
The code assumes that dimy (the number of rows) is 3 but never checks it. If it is smaller, expect crashes indeed.
Also notes that there's a bug here:
ptrOutput[ 0 + dimy * i ] = cloud_filtered->points[i].x;
ptrOutput[ 0 + dimy * i ] = cloud_filtered->points[i].y;
ptrOutput[ 0 + dimy * i ] = cloud_filtered->points[i].z;
It should be 0+, 1+, and 2+. That bug wouldn't cause a crash though.

Sign in to comment.

Answers (0)

Asked:

on 30 Aug 2019

Edited:

on 30 Aug 2019

Community Treasure Hunt

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

Start Hunting!