Memory problems with Cell Array mex file
Show older comments
I got a memory issue with the following piece of mex-file codes. The codes do yield correct results and without crash in Debug mode in visual studio 2022, but I got a crash when running them in Release mode with errors about heap memory issue related to the variable vOut. Here vOut is of type std::vector<double> and is the return value of a function. I have tried different ways to detect the problem, but could not solve it. I greatly appreciate if anyone can help me with the issue.
Below is the extracted codes that cause the problem:
std::vector<double> vOut;//Input argument of the function and is passed by reference. It's used to store returning result.
//Input
mmxArray* mxToolID = mxCreateDoubleMatrix(1, 1, mxREAL);
*(double *)mxGetPr(mxToolID) = (double)nToolID;// nToolID is an input of type int
//Ouputs
int nargout = 1;
mxArray *mxNP = mxCreateCellMatrix(1, nargout);
//this function is from an external dll file generated from matlab
bool retval = mlfSimulator(1, &mxNP, mxToolID);
//Decode the ouput mxNP which contains a cell array with each cell being a 2D matrix
mxArray* mxOut = mxGetCell(mxNP, 0);
if (mxOut != nullptr)
{
size_t n_cell = mxGetNumberOfElements(mxOut);
for (size_t k = 0; k < n_cell; k++)
{
mxArray* cell = mxGetCell(mxOut, k);// cell is a 2D matrix
double* ptr = mxGetPr(cell);
if (ptr != nullptr)
{
size_t rowNum = mxGetM(cell);
size_t columnNum = mxGetN(cell);
for (size_t i = 0; i < rowNum; i++)
{
for (size_t j = 0; j < columnNum; j++)
{
size_t loc = j * rowNum + i;
vOut.push_back(ptr[loc]);
}
}
}
}
}
if (mxToolID != nullptr) mxDestroyArray(mxToolID);
if (mxNP != nullptr) mxDestroyArray(mxNP);
5 Comments
Bruno Luong
on 14 Jul 2023
Edited: Bruno Luong
on 14 Jul 2023
I'm not sure but I have some doubt
mxArray* cell = mxGetCell(mxOut, k);// cell is a 2D matrix
double* ptr = mxGetPr(cell);
cell can be NULL, in this case it seems mxGetPr(NULL) migh not be a valid call. Similar for mxGetM(cell); mxGetN(cell);
James Tursa
on 14 Jul 2023
Edited: James Tursa
on 14 Jul 2023
Bruno has likely pointed out the issue (+1), and I will double down on that. These lines are certainly suspect:
mxArray *mxNP = mxCreateCellMatrix(1, nargout); // All elements of mxNP are NULL
bool retval = mlfSimulator(1, &mxNP, mxToolID); // Elements of mxNP filled in here?
mxArray* mxOut = mxGetCell(mxNP, 0);
if (mxOut != nullptr)
{
size_t n_cell = mxGetNumberOfElements(mxOut);
for (size_t k = 0; k < n_cell; k++)
{
mxArray* cell = mxGetCell(mxOut, k);// cell is a 2D matrix
// Maybe you should check cell to make sure it is not NULL here???
double* ptr = mxGetPr(cell);
You as the programmer must fill the cell array elements with valid mxArray pointers before you can use them and dereference them. You haven't shown us enough code to definitively track down the problem since you haven't shown us the code that fills in the mxNP elements. What is going on in mlfSimulator( )? Until you show us this code we can't track down your problem, but as a guess it sure looks like maybe not all of the mxNP elements got filled with valid mxArray pointers. And there are lots of other checks you could be doing to make your code more robust, like ensuring that cell is a real double full 2D matrix, etc.
I would note that the following code does not store the elements in the same memory order. I.e., the memory order of the data in the 2D MATLAB matrices is different from the memory order of the data in your vOut variable. Maybe this doesn't make a difference to you, or it is intentional, but I thought I would point it out:
for (size_t i = 0; i < rowNum; i++)
{
for (size_t j = 0; j < columnNum; j++)
{
size_t loc = j * rowNum + i;
vOut.push_back(ptr[loc]);
}
}
As an aside, this:
mmxArray* mxToolID = mxCreateDoubleMatrix(1, 1, mxREAL);
*(double *)mxGetPr(mxToolID) = (double)nToolID;// nToolID is an input of type int
is a very convoluted way of just doing this:
mmxArray* mxToolID = mxCreateDoubleScalar(nToolID);
Accepted Answer
More Answers (0)
Categories
Find more on Call C++ from MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!