Thread Subject: mex memory leak

Subject: mex memory leak

From: Florian

Date: 3 Sep, 2007 13:12:45

Message: 1 of 2

Hello all,
I am trying my first bit of mex code, but somehow produce a
memory leak - could you please help?


plhs[0]=mxCreateDoubleMatrix(arr_si,1,mxREAL);
all_values = mxGetPr(plhs[0]);

/* Read from file */
while(read_from_file_func(&val))
{
     all_values[n] = val;
}

mxSetPr(plhs[0],all_values);

?? Do I use mxGetPr & mxSetPr correctly?
Thansk,
flo

Subject: mex memory leak

From: Dan

Date: 3 Sep, 2007 15:28:24

Message: 2 of 2

"Florian " <f.pappenberger@NOSPAMlancater.ac.uk> wrote in
message <fbh18d$6vc$1@fred.mathworks.com>...
> Hello all,
> I am trying my first bit of mex code, but somehow
produce a
> memory leak - could you please help?
>
>
> plhs[0]=mxCreateDoubleMatrix(arr_si,1,mxREAL);
> all_values = mxGetPr(plhs[0]);
>
> /* Read from file */
> while(read_from_file_func(&val))
> {
> all_values[n] = val;
> }
>
> mxSetPr(plhs[0],all_values);
>
> ?? Do I use mxGetPr & mxSetPr correctly?
> Thansk,
> flo

There is no need to use mxSetPr in this situation. Your
variable all_values is a pointer to the double matrix
plhs[0] so setting all_values[n] is setting the values for
plhs[0]. That might be causing the memory leak.

One other problem you may have is that you don't appear to
be increasing n so it looks like you are setting the same
value over and over again in your while loop.

Dan

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
mex Florian 3 Sep, 2007 09:15:04
memory Florian 3 Sep, 2007 09:15:04
compile Florian 3 Sep, 2007 09:15:04
rssFeed for this Thread

Contact us at files@mathworks.com