the documentation is a bit unclear about this.. when inside
mexFunction(), is it allowed to allocate memory using the c malloc
library function?
I am trying to do so and I am am getting weird heap corruption errors
when mexFunction is called the 2nd time. I am using the malloc'ed blocks
only within the mexFunction(), and the pointers are never returned to
Matlab.
The documentation says I *should* use the Matlab equivalents, but I
can't easily do so because I am using a 3rd party library.
Thanks in advance,
Gunnar
Are you freeing the memory after you're done using it? mxCalloc,
mxMalloc etc. free all allocated memory automatically when the MEX
file exits so you don't HAVE to call mxFree (unless you made the
memory persistent by calling mexMakeMemoryPersistent). Also, if you're
using the mexFunction interface you should have access to all those
functions, what do you mean when you say you can't use the mex
equivalents 'easily'?
Ashish.
Using malloc (or calloc or realloc) without freeing the memory will
not in and of itself result in memory corruption errors, it will
simply result in an out of memory error when the heap is exhausted
because MATLAB no longer has access to that memory. This is
particularly true if you repeatedly call the function that leaks
memory over-and-over as you indicate. If you are getting memory
corruption errors then I would suspect you are overwriting memory
array bounds somewhere in your code, or misusing a MATLAT mx___
function call. A 3rd party library should free up any temporary memory
it allocates, or it isn't written very well. Are you getting a pointer
returned from a function you are calling from this 3rd party library,
but not freeing this memory later on? Can you post some code for us to
look at?
And like the other poster already replied, code that you write should
always use mxMalloc, mxCalloc, and mxRealloc to avoid potential memory
leaks.
James Tursa
thank you very much for your replies.
>> the documentation is a bit unclear about this.. when inside
>> mexFunction(), is it allowed to allocate memory using the c malloc
>> library function?
>
> Using malloc (or calloc or realloc) without freeing the memory will
> not in and of itself result in memory corruption errors, it will
> simply result in an out of memory error when the heap is exhausted
> because MATLAB no longer has access to that memory.
I am aware of how memory allocation (and deallocation) in C works. My
problem is heap corruption, not memory leaking.
If it is okay to use malloc and free interally inside mexFunction, and
malloc'd (vs. mxAlloc) memory is not garbage collected when Matlab
regains control after mexFunction returns, then it must be a bug in my
code (or in the library's). I guess I have some more debugging to do..
Thanks again,
Gunnar
> If it is okay to use malloc and free interally inside mexFunction, and
> malloc'd (vs. mxAlloc) memory is not garbage collected when Matlab
> regains control after mexFunction returns, then it must be a bug in my
> code (or in the library's). I guess I have some more debugging to do..
You can use malloc/free without limitations. If your memory gets
corrupted, your code is buggy.
--
Ralph
5539 days of Linux experience.
>Gunnar Selke <1se...@informatik.uni-hamburg.de> writes:
>
>> If it is okay to use malloc and free interally inside mexFunction, and
>> malloc'd (vs. mxAlloc) memory is not garbage collected when Matlab
>> regains control after mexFunction returns, then it must be a bug in my
>> code (or in the library's). I guess I have some more debugging to do..
>
>You can use malloc/free without limitations.
No. You can use them, but there *are* limitations. Namely the memory
will leak if you execute a mexErrMsgTxt call inbetween the malloc and
the free calls. That's what I had pointed out in an earlier post.
James Tursa
> No. You can use them, but there *are* limitations. Namely the memory
> will leak if you execute a mexErrMsgTxt call inbetween the malloc and
> the free calls. That's what I had pointed out in an earlier post.
If that can happen, then the code is buggy.
--
Ralph
You are still missing the point, which is this:
It is *impossible* to write mex code that mixes
malloc/calloc/realloc with any of the mx routines that
allocate memory (e.g., mxCreate...) or with the
mexErrMsgTxt routine without risking a memory leak unless
*all* of the malloc/calloc/realloc memory is freed before
you make any of the mx calls. That is why this statement
you made in an earlier post does not make any sense to me:
>You can use malloc/free without limitations.
I would consider the restriction to never mix the calls in
the way I described a very real and important limitation.
For instance, consider the following example:
1) mxArray *mx;
2) double *dp;
3) dp = (double *) calloc(100*200, sizeof(double));
4) if( dp == NULL )
5) do something to handle error here;
6) mx = mxCreateDoubleMatrix(100, 200, mxREAL);
7) if( mx == NULL ) {
8) free(dp);
9) mexErrMsgTxt("Unable to create mx");
10) }
11) // some code here to use mx and dp
12) mxDestroyArray(mx);
13) free(dp);
14) // rest of your code
Looks like line 8 will cover me and prevent a memory leak
if the mxCreateDoubleMatrix allocation doesn't work,
right? Wrong. Line 8 and 9 will never be executed under
any conditions in a mex routine. In a mex routine, if the
mxCreateDoubleMatrix call fails then control is returned
directly to MATLAB after the memory allocated from
previous MATLAB mx routines such as mxMalloc, mxCalloc,
mxCreate..., etc. is freed. Line 7 will never be reached
if mx is NULL. The memory that dp points to is leaked in
this case.
Now, I don't have a particular problem with calling my
example code above buggy, since it can produce a memory
leak. But I do have a problem with saying
malloc/calloc/realloc can be used without limitations.
They can't.
James Tursa
> You are still missing the point, which is this:
I don't miss the point. Instead, I'm fully aware of it. But this is
all very well explained in the Matlab API documentation. Your example
is the typical code of an inexperienced MEX-file programmer who (maybe)
has read the Matlab API documentation but neither does understand it
nor is able to transliterate it into an error-free program. Again,
utilizing malloc/free within a MEX-file is totally legal and they can
be used without limitations. C gives you enough rope to hang yourself,
but nobody said that you shall actually do it.
--
Ralph
Hi
question: what happens when the user tries presses Ctrl-C (or equivalent)? Can your mex code be interrupted at any point (e.g. between malloc and free calls)? If so, you'll have a memory leak. But maybe matlab only breaks when you call a mx function?
Thanks
Kris
> question: what happens when the user tries presses Ctrl-C (or
> equivalent)? Can your mex code be interrupted at any point
> (e.g. between malloc and free calls)? If so, you'll have a memory
> leak. But maybe matlab only breaks when you call a mx function?
Hello Kris,
For such a case, you depend on how Matlab handles the signal.
I've only once investigated the behavior of a C-MEX S-function
in case the user interrupts a running simulation with Ctrl-C.
The C-MEX S-function under investigation did extensive data
logging of Simulink signals. More than 500 instances of the
C-MEX S-function wrote approx. 10000 Simulink signals at each
major simulation time step into a HDF5 file. All the memory
for the internal data structures was allocated with malloc.
My tests showed that it was not possible to generate a memory
leak as long as Matlab continued to run.
Here is the general plan for how to use malloc/free
successfully in Matlab:
1. Initialize all pointers
2. Install a cleanup procedure (and make sure
it is called in case of an error)
3. Allocate memory
4. Work real hard
5. Call the cleanup procedure unless Matlab
does it for you automatically
In the cleanup procedure
1. Free allocated memory
2. Reinitialize pointers
--
Ralph Schleicher <http://ralph-schleicher.de>
Development * Consulting * Training
Mathematical Modeling and Simulation
Software Tools
Recently I also faced this problem when I failed to use malloc.
I was trying to pass an array of structure, for example
//c-style structure
typedef struct A {
int a1,
int a2
};
Now I pass Matlab structure from matlab to mex function(c) through this way
a(1).a1 = 1;
a(1).a1 = 2;
a(2).a1 = 3;
a(4).a1 = 4;
I wanted use malloc to dynamically allocate memory for my structure A.
And it told me undeclared identified with this statement
A *temp; //A is already defined as struct above
temp = (A*)malloc(sizeof(A);
Above syntax did not work out, but if you write in this way
struct A *temp;
temp = (struct A*)malloc (sizeof(struct A));
Using struct infront of A, worked out.
I hope this could be useful for others people who search for Mex Malloc Memory allocation issue.
Ajay Pratap Singh apts
ps:. please let me know if it was helpful
Gunnar Selke <gse...@gunnar.info> wrote in message <478a9107$1...@news.arcor-ip.de>...