mex out of memory
1 回表示 (過去 30 日間)
古いコメントを表示
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
2016 年 8 月 5 日
Ronron - you've commented out mxFree. Why after allocating memory using mxMalloc?
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 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!