【问题标题】:How can I free memory allocated by mxGetData()如何释放 mxGetData() 分配的内存
【发布时间】:2017-02-28 23:03:03
【问题描述】:

我正在将一个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中的断点。

【问题讨论】:

    标签: c++ matlab mex


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      调用 mxGetData() 时。该函数唯一要做的就是返回一个指向存储在 mxArray 中的真实(与虚构相反)数据的指针。

      因此无需释放内存,因为在此调用期间没有动态分配任何内容。

      【讨论】:

      • 我明白你在说什么。然后应该删除 mxArray 以释放内存。但是当在那个 mxArray 程序上执行 mxFree() 时会触发一个断点。
      • @AliyaClark 你试过mxFree((void*)pa) 吗?但是,请注意,如果您释放内存,您将不再拥有数据
      • 正如@Ander Biguri 所说,使用 mxDestroyArray 而不是 mxFree
      • @yar 我试过了,效果很好。感谢您的支持。
      猜你喜欢
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 2011-05-13
      • 2015-07-30
      相关资源
      最近更新 更多