如何释放由mxGetData()分配的内存

4

我正在将一个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中触发一个断点。
2个回答

3

edit([matlabroot '/extern/examples/eng_mat/matdgns.c']); 的 MATLAB 演示中,建议使用 mxDestroyArray 代替 mxFree


1
当调用mxGetData()时,该函数唯一的作用就是返回指向存储在mxArray中真实(与虚构相对)数据的指针。因此,在此调用期间不需要释放内存,因为没有动态分配任何内容。

我理解你所说的。那么为了释放内存,mxArray应该被删除。但是当对该mxArray执行mxFree()时,程序触发了断点。 - Aliya Clark
@AliyaClark 你尝试过 mxFree((void*)pa) 吗?但是请注意,如果你释放了内存,你将不再拥有数据。 - Ander Biguri
1
正如 @Ander Biguri 所说,应该使用 mxDestroyArray 而不是 mxFree。 - yar
@yar 我试过了,运行得很好。感谢您的支持。 - Aliya Clark

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接