Skip to content

Commit

Permalink
fix leak in mex (#1021)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-numerical-software committed Dec 1, 2023
1 parent bfb8344 commit 45de3ea
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
25 changes: 15 additions & 10 deletions modules/mex/src/cpp/MxArrayOf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace Nelson {
static mwSize*
GetDimensions(const ArrayOf& array, mwSize& numdims)
{
numdims = (int)array.nDims();
auto* dim_vec = (mwSize*)mxCalloc(numdims, sizeof(mwSize));
numdims = (mwSize)array.nDims();
auto* dim_vec = (mwSize*)malloc(numdims * sizeof(mwSize));
if (dim_vec != nullptr) {
for (mwSize i = 0; i < numdims; i++) {
dim_vec[i] = array.getDimensions()[i];
Expand Down Expand Up @@ -90,7 +90,7 @@ ArrayOfComplexToMexArray(const ArrayOf& array, mxClassID classID, bool interleav
mwSize num_dim;
mwSize* dim_vec = GetDimensions(array, num_dim);
mxArray* ret = mxCreateNumericArray(num_dim, dim_vec, classID, mxCOMPLEX);
mxFree(dim_vec);
free(dim_vec);
dim_vec = nullptr;
if (ret) {
auto* sp = (nlsType*)array.getDataPointer();
Expand All @@ -115,6 +115,7 @@ ArrayOfRealToMexArray(const ArrayOf& array, mxClassID classID)
mwSize num_dim;
mwSize* dim_vec = GetDimensions(array, num_dim);
mxArray* ret = mxCreateNumericArray(num_dim, dim_vec, classID, mxREAL);
free(dim_vec);
dim_vec = nullptr;
if (ret) {
auto* sp = (nlsType*)array.getDataPointer();
Expand Down Expand Up @@ -529,7 +530,8 @@ ArrayOfToMxArray(const ArrayOf& nlsArrayOf, bool interleavedComplex)
mwSize num_dim;
mwSize* dim_vec = GetDimensions(nlsArrayOf, num_dim);
res->number_of_dims = num_dim;
res->dims = dim_vec;
res->dims = copyDims(num_dim, dim_vec);
free(dim_vec);
res->classID = mxOBJECT_CLASS;
res->issparse = false;
res->interleavedcomplex = interleavedComplex;
Expand All @@ -552,7 +554,8 @@ ArrayOfToMxArray(const ArrayOf& nlsArrayOf, bool interleavedComplex)
mwSize num_dim;
mwSize* dim_vec = GetDimensions(nlsArrayOf, num_dim);
res->number_of_dims = num_dim;
res->dims = dim_vec;
res->dims = copyDims(num_dim, dim_vec);
free(dim_vec);
res->classID = mxOBJECT_CLASS;
res->issparse = false;
res->interleavedcomplex = interleavedComplex;
Expand All @@ -573,8 +576,7 @@ ArrayOfToMxArray(const ArrayOf& nlsArrayOf, bool interleavedComplex)
mwSize num_dim;
mwSize* dim_vec = GetDimensions(nlsArrayOf, num_dim);
res = mxCreateCellArray(num_dim, dim_vec);
mxFree(dim_vec);
dim_vec = nullptr;
free(dim_vec);
if (res != nullptr) {
auto* sp = (ArrayOf*)nlsArrayOf.getDataPointer();
auto** dp = (mxArray**)res->realdata;
Expand All @@ -595,7 +597,8 @@ ArrayOfToMxArray(const ArrayOf& nlsArrayOf, bool interleavedComplex)
mwSize num_dim;
mwSize* dim_vec = GetDimensions(nlsArrayOf, num_dim);
res->number_of_dims = num_dim;
res->dims = dim_vec;
res->dims = copyDims(num_dim, dim_vec);
free(dim_vec);
res->classID = mxSTRUCT_CLASS;
res->issparse = false;
res->interleavedcomplex = interleavedComplex;
Expand All @@ -616,7 +619,8 @@ ArrayOfToMxArray(const ArrayOf& nlsArrayOf, bool interleavedComplex)
mwSize num_dim;
mwSize* dim_vec = GetDimensions(nlsArrayOf, num_dim);
res->number_of_dims = num_dim;
res->dims = dim_vec;
res->dims = copyDims(num_dim, dim_vec);
free(dim_vec);
res->classID = mxSTRUCT_CLASS;
res->issparse = false;
res->interleavedcomplex = interleavedComplex;
Expand Down Expand Up @@ -695,7 +699,8 @@ ArrayOfToMxArray(const ArrayOf& nlsArrayOf, bool interleavedComplex)
mwSize num_dim;
mwSize* dim_vec = GetDimensions(nlsArrayOf, num_dim);
res->number_of_dims = num_dim;
res->dims = dim_vec;
res->dims = copyDims(num_dim, dim_vec);
free(dim_vec);
res->classID = mxOBJECT_CLASS;
res->issparse = false;
res->interleavedcomplex = interleavedComplex;
Expand Down
4 changes: 2 additions & 2 deletions modules/mex/src/cpp/MxHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ countElements(mwSize ndim, const mwSize* dims)
mwSize*
copyDims(mwSize ndim, const mwSize* dims)
{
auto* p = (mwSize*)mxMalloc(sizeof(mwSize) * ndim);
auto* p = (mwSize*)malloc(sizeof(mwSize) * ndim);
if (p != nullptr) {
for (mwSize i = 0; i < ndim; i++) {
p[i] = dims[i];
Expand Down Expand Up @@ -178,7 +178,7 @@ mwSize*
GetDimensions(const Nelson::ArrayOf& array, mwSize& numdims)
{
numdims = (mwSize)(array.nDims());
auto* dim_vec = (mwSize*)mxMalloc(sizeof(mwSize) * numdims);
auto* dim_vec = (mwSize*)malloc(sizeof(mwSize) * numdims);
for (mwSize i = 0; i < numdims; i++) {
dim_vec[i] = array.getDimensions()[i];
}
Expand Down
14 changes: 10 additions & 4 deletions modules/mex/src/cpp/MxMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mxIsRegisteredPointer(void* ptr)
static void
registerMexPointer(void* ptr)
{
if (ptr != nullptr) {
if (ptr != nullptr && !mxIsRegisteredPointer(ptr)) {
registeredMxPointers.insert(ptr);
}
}
Expand Down Expand Up @@ -121,6 +121,7 @@ mxDestroyArray(mxArray* pm)
mxArray* p = gp[i];
mxDestroyArray(p);
}
pm->realdata = nullptr;
}
if (pm->classID == mxOBJECT_CLASS) {
auto* ptr = (Nelson::ArrayOf*)pm->ptr;
Expand All @@ -137,8 +138,11 @@ mxDestroyArray(mxArray* pm)
mxFree(pm->Ir);
}
mxFree(pm->realdata);
pm->realdata = nullptr;
mxFree(pm->imagdata);
mxFree(pm->dims);
pm->imagdata = nullptr;
free(pm->dims);
pm->dims = nullptr;
mxFree(pm);
}
}
Expand All @@ -159,7 +163,8 @@ mxDuplicateArray(const mxArray* in)
mwSize num_dim;
mwSize* dim_vec = GetDimensions(*inPtr, num_dim);
ret->number_of_dims = num_dim;
ret->dims = dim_vec;
ret->dims = copyDims(num_dim, dim_vec);
free(dim_vec);
ret->classID = mxOBJECT_CLASS;
ret->interleavedcomplex = in->interleavedcomplex;
ret->issparse = in->issparse;
Expand Down Expand Up @@ -210,7 +215,8 @@ mxDuplicateArray(const mxArray* in)
mwSize num_dim;
mwSize* dim_vec = GetDimensions(*inPtr, num_dim);
ret->number_of_dims = num_dim;
ret->dims = dim_vec;
ret->dims = copyDims(num_dim, dim_vec);
free(dim_vec);
ret->classID = mxSTRUCT_CLASS;
ret->interleavedcomplex = in->interleavedcomplex;
ret->issparse = false;
Expand Down
3 changes: 2 additions & 1 deletion modules/mex/src/cpp/MxStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ mxCreateStructArray(mwSize ndim, const mwSize* dims, int nfields, const char** f
mwSize num_dim;
mwSize* dim_vec = GetDimensions(s, num_dim);
ret->number_of_dims = num_dim;
ret->dims = dim_vec;
ret->dims = copyDims(num_dim, dim_vec);
free(dim_vec);
ret->classID = mxSTRUCT_CLASS;
ret->issparse = false;
ret->iscomplex = false;
Expand Down

0 comments on commit 45de3ea

Please sign in to comment.