Matlab crashing when evaluating mex
显示 更早的评论
I have the following C code, that I compile into a mex (32bit) with Visual Studio Express 2013:
#include <stdlib.h>
#include <stdio.h>
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
const size_t m = mxGetM(prhs[0]);
const size_t n = mxGetN(prhs[0]);
double** x = (double**)mxCalloc(n, m);
for (size_t i = 0; i < n; i++)
x[i] = (double*)mxCalloc(m, sizeof(double));
for (size_t i = 0; i < n; i++)
mxFree(x[i]);
mxFree(x);
}
It is basically just allocating space to store a 2D matrix passed from Matlab, then deallocating it (without setting any value). When I compile it into mexw32 with Visual Studio Express 2013, then execute the following in Matlab 2013b 32bit, it crashes Matlab:
n = 51; x = zeros(3,n); myMex(x)
Here's the top of the stack log from the crash:
Stack: [0x00430000,0x00c30000], sp=0x00c23f7c, free space=8143k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C [ntdll.dll+0x2e43e] RtlInitUnicodeString+0x196
C [ntdll.dll+0x2e0a3] RtlFreeHeap+0x7e
C [kernel32.dll+0x114bd] HeapFree+0x14
C [MSVCR100.dll+0x1016a] free+0x1c
Does anyone have any idea whatI'm doing wrong here? Is it down to the way I deallocate each pointer?
Thanks in advance for your answers.
采纳的回答
更多回答(1 个)
const size_t m = mxGetM(prhs[0]);
const size_t n = mxGetN(prhs[0]);
Now m and n are 1, because the first input is a scalar.
In
double** x = (double**)mxCalloc(n, m);
you allocate memory for n elements of size m bytes, which is 1 byte.
x[i] = (double*)mxCalloc(...)
writes a pointer to a double, which has 32 or 64 bits depending on the system, to an element you reserved 1 byte for. This should crash Matlab.
Try this:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
const size_t m = (size_t) mxGetData(prhs[0]);
const size_t n = (size_t) mxGetData(prhs[1]);
double** x = (double**)mxCalloc(n, sizeof(double *));
for (size_t i = 0; i < n; i++)
x[i] = (double*) mxCalloc(m, sizeof(double));
for (size_t i = 0; i < n; i++)
mxFree(x[i]);
mxFree(x);
}
4 个评论
MaFo
2016-10-28
James Tursa
2016-10-28
编辑:James Tursa
2016-10-28
I think Jan had a typo. Go back to this:
const size_t m = mxGetM(prhs[0]);
const size_t n = mxGetN(prhs[0]);
That being said, this is still somewhat messy code IMO. What is the ultimate goal here? Will you be copying data back & forth between mxArray variables and your dynamically allocated memory? Are you just trying to learn how to use the double bracket [ ][ ] syntax with allocated memory? Or what? There may be better ways of going about things depending on what the end goal is. (e.g., with your method the data for this 2D matrix is not guaranteed to be contiguous in memory, but there are other methods of allocation that will guarantee this)
MaFo
2016-11-2
类别
在 帮助中心 和 File 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!