I haven't looked at your code in detail, but I do have a couple of comments based on a cursory look.
1) It appears to me that your architecture is set up so that the only place the new object handle is kept is in a returned MATLAB variable (uint64). This puts a heavy reliance on the user to NEVER lose that variable. If they did then your routine could not recover. It is locked and there is no way to unlock it because there is no way to pass in a valid handle (via the uint64) to get it unlocked. I would advise keeping track of that handle in your mex routine (e.g., a top level variable) so that if you need to clear the mex function for some reason and you don't have the proper uint64 to pass in, you can still do so (e.g., add another input command so that the mex function can delete the object on its own and unlock itself).
2) It looks like this loop is intended to copy data from an mxArray variable into a C++ vector:
for (long int nn=0; nn<NRow; nn++){
for (long int mm=0; mm<NCol; mm++){
Vec.push_back((double)mxGetPr(Cons_prhs)[0]);
mxGetPr(Cons_prhs)[0]+=1;
}
}
But what looks to me like your "advance to next value" logic doesn't make sense. mxGetPr(Cons_prhs)[0] is the double value itself, not some pointer. So you end up incrementing the first element of the original mxArray by 1 each iteration, rather than stepping through the double elements. I think you meant something more like this instead:
double *pr = mxGetPr(Cons_prhs);
for (long int nn=0; nn<NRow; nn++){
for (long int mm=0; mm<NCol; mm++){
Vec.push_back(*pr++);
}
}
3) This memory copy doesn't look right either:
mxArray * IntVector2MX(const std::vector<int>& v){
mxArray *mx;
mexPrintf("\n IntVector2MX: Pointer defined.");
mx = mxCreateNumericMatrix(1, v.size(),mxINT32_CLASS, mxREAL);
mexPrintf("\n IntVector2MX: Numeric array created.");
std::copy(v.begin(), v.end(), mxGetPr(mx));
You are attempting to copy 32-bit integers, but mxGetPr returns a (double *) type, not a (int *) type. So I think you will end up calling the wrong copy method because it thinks it needs to copy into double memory. I can easily see this writing off the end of the array and crashing. Try this instead:
std::copy(v.begin(), v.end(), (int *)mxGetData(mx));
Best Answer