Azzera filtri
Azzera filtri

mex out of memory

2 visualizzazioni (ultimi 30 giorni)
Ronron
Ronron il 5 Ago 2016
Modificato: James Tursa il 5 Ago 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 Commenti
Geoff Hayes
Geoff Hayes il 5 Ago 2016
Ronron - you've commented out mxFree. Why after allocating memory using mxMalloc?
James Tursa
James Tursa il 5 Ago 2016
Modificato: James Tursa il 5 Ago 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?

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Write C Functions Callable from MATLAB (MEX Files) in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by