mex out of memory

1 次查看(过去 30 天)
Ronron
Ronron 2016-8-5
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 个评论
Geoff Hayes
Geoff Hayes 2016-8-5
Ronron - you've commented out mxFree. Why after allocating memory using mxMalloc?
James Tursa
James Tursa 2016-8-5
编辑:James Tursa 2016-8-5
@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?

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by