Filter löschen
Filter löschen

mex out of memory

2 Ansichten (letzte 30 Tage)
Ronron
Ronron am 5 Aug. 2016
Bearbeitet: James Tursa am 5 Aug. 2016
Hello, I know there are already several question about out of memory in combination with mex. When i call the mex function the first time no problem, after that out of memory. Looks like a memory leak. But i don't know why. Maybe I can get some help here? In Matlab I just call the mex-Function in the command window. Following I will post the code snippet where it accurs.
#include "urg_sensor.h"
#include "urg_utils.h"
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include "mex.h"
#pragma comment(lib,"wsock32.lib")
int getDistance(long* data);
void setReturnVector(mxArray *plhs[], int index, long* vector);
static int initialized = 0;
static urg_t *urg;
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
...
long *data = NULL;
int n;
data = (long *)mxMalloc(urg_max_data_size(urg) * sizeof(data[0]));
n = getDistance(data);
setReturnVector(plhs, 0, data);
//mxFree(data);
}
int getDistance(long *data){
long time_stamp;
int n;
mexPrintf("LaserScanner: Measuring...\n");
n = urg_get_distance(urg, data, &time_stamp);
return n;
}
void setReturnVector(mxArray *plhs[], int index, long* vector){
plhs[index] = mxCreateNumericMatrix(0,0,mxINT32_CLASS, mxREAL);
mxSetData(plhs[index], vector);
mxSetM(plhs[index], (int) urg_max_data_size(urg)/2);
mxSetN(plhs[index], 1);
}
  2 Kommentare
Geoff Hayes
Geoff Hayes am 5 Aug. 2016
Ronron - you've commented out mxFree. Why after allocating memory using mxMalloc?
James Tursa
James Tursa am 5 Aug. 2016
Bearbeitet: James Tursa am 5 Aug. 2016
@Geoff: The mxFree needs to be commented out because the data address is being attached to an mxArray data area via the setRetrunVector call and the mxSetData(plhs[index],vector) line. Having mxFree(data) execute would invalidate plhs[0] and likely crash MATLAB.
@Ronron: Is your posted code the only code that allocates any memory? Do you actually get an "out of memory" error message on the screen?

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by