Search code examples
c++matlabmex

How can I free memory allocated by mxGetData()


I am importing a mat file to my C++ code. After importing data, performing calculations and saving to another place I want to free the memory occupied by the original data.

Is there any specific function to perform that. Would just deleting the pointer returned by mxGetData() free the memory?

This is the class I have created to import mat file

#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

The following is the cpp implementation

#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);

}

Here when I delete the object program trigger a break point in the file free.c.


Solution

  • The MATLAB demo on edit([matlabroot '/extern/examples/eng_mat/matdgns.c']); seems to suggest using mxDestroyArray instead of mxFree.