From: Fabio Pinna on
Hi,
I've been working for a while on a set of c++ mex file and I recently discovered a bug.
For some particular input data the mex file is causing matlab to crash.
I saved the data set and tried to reproduce the error just in c++, which didn't happen.

This is fairly strange as the mex wrapper is nothing more than an entry point for matlab and all the computations are done inside the (separete) c++ code.

This problem is multiplatform

Any idea?
From: James Tursa on
"Fabio Pinna" <fabio.pinna(a)vki.ac.be-remove.this> wrote in message <hsbo4l$35c$1(a)fred.mathworks.com>...
> Hi,
> I've been working for a while on a set of c++ mex file and I recently discovered a bug.
> For some particular input data the mex file is causing matlab to crash.
> I saved the data set and tried to reproduce the error just in c++, which didn't happen.
>
> This is fairly strange as the mex wrapper is nothing more than an entry point for matlab and all the computations are done inside the (separete) c++ code.
>
> This problem is multiplatform
>
> Any idea?

We need to see your code. You are likely using an invalid pointer.

James Tursa
From: Fabio Pinna on
Thank you for your reply!
Well the core routines are pretty nasty to understand.
Here I post the wrap

#include "kdtree2.hpp"
#include "mexFunction.h"
#include <math.h>

#include "boost/multi_array.hpp"
#include "boost/random.hpp"
#include "mex.h"

void mexFunction(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[]){

kdtree2_result_vector result;

// chec number of arguments
if( nrhs!=3 )
mexErrMsgTxt("This function requires 3 arguments\n");
if( !mxIsNumeric(prhs[0]) )
mexErrMsgTxt("varargin{0} must be a valid kdtree pointer\n");
if( !mxIsNumeric(prhs[1]) )
mexErrMsgTxt("varargin{1} must be a query point\n");
if( !mxIsNumeric(prhs[2]) )
mexErrMsgTxt("varargin{2} must be a scalar integer\n");

// retrieve the tree pointer
kdtree2* tree;
//retrieve_tree( prhs[0], tree );
double* pointer0;
pointer0 = mxGetPr(prhs[0]);
long pointer1 = (long) pointer0[0];
// convert it to "KDTree"
tree = (kdtree2*) pointer1;

// check that I actually received something
if( tree == NULL )
mexErrMsgTxt("vararg{1} must be a valid k-D tree pointer\n");

// retrieve the query point
vector<float> query(tree->dim,0); // query is only one point!
double* data = mxGetPr(prhs[1]);
for(int it=0; it < tree->dim; it++){
query[it] = data[it];
}

// retrieve the query cardinality
int k=0;
//retrieve_k( prhs[2], k );
k = mxGetScalar(prhs[2]);


tree->n_nearest(query,k,result);

// return the indexes
plhs[0] = mxCreateDoubleMatrix(k, 1, mxREAL);
double* indexes = mxGetPr(plhs[0]);
double* output = mxGetPr(plhs[0]);
for (int i=0; i < k; i++){
indexes[ i ] = result[i].idx + 1;
}

}


Anyway if the problem is related to a bad pointer it shouldn't even call correctly the routines in other cases. What really happens is the crash inside the c++ routine (only used with matlab but not with pure c++ main)

Thank you for your comments
From: Jan Simon on
Dear Fabio!

Could you be so kind and mention the line where your C++ function crashes?
You could ask your debugger or set some "mexPrintf" or "return" statements to locate your problem.

Kind regards, Jan
From: Fabio Pinna on
Hi Jan,
the line is:

tree->n_nearest(query,k,result);

So it crashes inside the c++ function not while calling it. I could use a "mexPrintf" inside a try-catch block in c++ but this is not allowed in matlab. That's why I first tried to debug the function in C++ with the same dataset and I discoverd that this way it was working flawlessly.
Thank you for your support.

Fabio


"Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <hsjli0$cvc$1(a)fred.mathworks.com>...
> Dear Fabio!
>
> Could you be so kind and mention the line where your C++ function crashes?
> You could ask your debugger or set some "mexPrintf" or "return" statements to locate your problem.
>
> Kind regards, Jan