Matlab crashing when evaluating mex
Show older comments
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.
Accepted Answer
More 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 Comments
MaFo
on 28 Oct 2016
James Tursa
on 28 Oct 2016
Edited: James Tursa
on 28 Oct 2016
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
on 2 Nov 2016
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!