c++ - 如何释放 mxGetData() 分配的内存

标签 c++ matlab mex

我正在将一个 mat 文件导入到我的 C++ 代码中。导入数据、计算并保存到另一个地方后,我想释放原始数据占用的内存。

是否有任何特定功能可以执行此操作。只删除 mxGetData() 返回的指针会释放内存吗?

这是我创建的用于导入 mat 文件的类

#ifndef READMAT_H
#define READMAT_H
#include "mat.h"
#include "matrix.h"
#include "mex.h"
#include "program_exception.h"
#include "stdint.h"

class readmat
{
private:
    const size_t *dimarray;
    const char **dir;
    MATFile *pmat;
    int ndir;
    mxArray *painfo,*pa;
    const char *file;
    int minute;
    int second;
    const char *name;
    const char *name1;
    bool isdouble;
    no_mat_exception noMAT;
public:

    //with default value
    readmat(const char *f);
    //  get number of dimensions
    int getnumbrofdimensions() const;
    // get pointer to array
    void*  getarraypointer() const;
    // get pointer to each dimension size
    const size_t* dimensionpointer() const;
    //number of elements
    int numberofelements() const;
    ~readmat();

};

#endif

以下是cpp实现

#include "readmat.h"
#include <iostream>
#include "mat.h"
#include "matrix.h"
#include "mex.h"
using namespace std;

// set the file name
readmat::readmat(const char *f)
{
    file = f;
    pmat = matOpen(file, "r");
    if (pmat == NULL) {
        throw noMAT;
    }
    else
    {
        dir = (const char **)matGetDir(pmat, &ndir);
        if (dir == NULL) {
            printf("Error reading directory of file %s\n", file);

        }
        else if (ndir > 1)
        {
            cout << "The number of variables are larger than 1" << endl;
        }
        else
        {
            mxFree(dir);
            matClose(pmat);
            pmat = matOpen(file, "r");
            if (pmat == NULL) {
                throw noMAT;
            }
            else
            {
                painfo = matGetNextVariableInfo(pmat, &name);
                matClose(pmat);
            }
            pmat = matOpen(file, "r");
            if (pmat == NULL) {
                throw noMAT;
            }
            else
            {
                pa = matGetNextVariable(pmat, &name1);
                matClose(pmat);
            }
        }

    }

}



int readmat::getnumbrofdimensions() const
{

    return mxGetNumberOfDimensions(painfo);
}

void* readmat::getarraypointer() const
{
    //return mxGetPr(pa);
    return mxGetData(pa);
}

const size_t*  readmat::dimensionpointer() const
{
    return mxGetDimensions(painfo);
}

int readmat::numberofelements() const
{
    return mxGetNumberOfElements(painfo);
}
readmat::~readmat()
{
    mxFree(pa);
    mxFree(painfo);

}

这里,当我删除目标程序时,会在文件 free.c 中触发一个断点。

最佳答案

edit([matlabroot '/extern/examples/eng_mat/matdgns.c']); 上的 MATLAB 演示似乎建议使用 mxDestroyArray而不是 mxFree

关于c++ - 如何释放 mxGetData() 分配的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42520385/

相关文章:

c++ - 删除数组或释放内存,C++ 错误

c++ while从具有不同数据类型的文件循环

matlab - 在 MATLAB 上使用基于矩阵的方法创建 cos 图?

matlab - 从自身内部删除 MATLAB 脚本是否安全?

matlab - K 表示在肘部图为平滑曲线时找到肘部

c++ - 在 MEX 中超快地将二进制文件写入磁盘

c++ - C/C++ 常量

php - 编写我的 linux 守护进程的首选方法是什么?

c - 根据 2 级 C-mex S-Function 中的导入数据类型设置输出端口的 Simulink block 数

c++ - QT exec() 命令崩溃