Thread Subject: Memory behaviour when calling built in functions fro a MEX file

Subject: Memory behaviour when calling built in functions fro a MEX file

From: Jes?s

Date: 24 Nov, 2009 09:09:06

Message: 1 of 3

Hi:

I am writing a mex file, where I am calling a built-in MATLAB function. I am
calling the function "single" to convert a matrix from double (8 bytes) to
single (4 bytes).

I have several questions about what happpens with the memory used in the parameters
to call the function (arguments) and the parameters that the function goes back (results).

I do this here:

     CallMatlabStatus= mexCallMATLAB(1,results,1,arguments,"single");

Then


arguments[0] contains the mxArray with "double" data.

results[0] contains the mxArray with "single" data.

Then I have the following questions


1) Can I delete ALL the data in arguments[0]. If I call:

mxDestroyArray(arguments[0]) destroys all the data?. I am not sure, but I think
thay this destroys the mxArray object but not the memory allocated for containing all
the matrix.



2) I have the same question for the results[0]. I am not sure if the results[0] mxArray and
the data (memory allocated) containing the matrix can be destroyed.

If I call:

myMatrix=mxGetpr(results[0));
mxDestroy(results[0]);


Them I can use myMatrix, but If I later want to delete the matrix

mxFree(myMatrix), then MATLAB gives an error and it hangs


Could you help me

jp

Subject: Memory behaviour when calling built in functions fro a MEX file

From: James Tursa

Date: 24 Nov, 2009 14:40:17

Message: 2 of 3

"Jes?s " <jpeinado.removethis@gmail.com> wrote in message <heg7ri$9lc$1@fred.mathworks.com>...
> Hi:
>
> I am writing a mex file, where I am calling a built-in MATLAB function. I am
> calling the function "single" to convert a matrix from double (8 bytes) to
> single (4 bytes).
>
> I have several questions about what happpens with the memory used in the parameters
> to call the function (arguments) and the parameters that the function goes back (results).
>
> I do this here:
>
> CallMatlabStatus= mexCallMATLAB(1,results,1,arguments,"single");
>
> Then
>
>
> arguments[0] contains the mxArray with "double" data.
>
> results[0] contains the mxArray with "single" data.
>
> Then I have the following questions
>
>
> 1) Can I delete ALL the data in arguments[0]. If I call:
>
> mxDestroyArray(arguments[0]) destroys all the data?. I am not sure, but I think
> thay this destroys the mxArray object but not the memory allocated for containing all
> the matrix.

mxDestroyArray(arguments[0]) destroys the entire array including the data. i.e., it frees the memory for the mxArray structure as well as all the memory in the pr and pi data areas.

> 2) I have the same question for the results[0]. I am not sure if the results[0] mxArray and
> the data (memory allocated) containing the matrix can be destroyed.
>
> If I call:
>
> myMatrix=mxGetpr(results[0));
> mxDestroy(results[0]);

(typo) mxDestroyArray(results[0]);

> Them I can use myMatrix, but If I later want to delete the matrix
>
> mxFree(myMatrix), then MATLAB gives an error and it hangs

Yes, it will hang because myMatrix no longer points to valid memory. Don't destroy the mxArray until you are done with it *and* done with the data in the pr and pi areas. Don't use mxFree on the data area of an mxArray.

James Tursa

Subject: Memory behaviour when calling built in functions fro a MEX file

From: Jes?s

Date: 25 Nov, 2009 08:31:20

Message: 3 of 3

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <hegr8h$ih7$1@fred.mathworks.com>...
> "Jes?s " <jpeinado.removethis@gmail.com> wrote in message <heg7ri$9lc$1@fred.mathworks.com>...
> > Hi:
> >
> > I am writing a mex file, where I am calling a built-in MATLAB function. I am
> > calling the function "single" to convert a matrix from double (8 bytes) to
> > single (4 bytes).
> >
> > I have several questions about what happpens with the memory used in the parameters
> > to call the function (arguments) and the parameters that the function goes back (results).
> >
> > I do this here:
> >
> > CallMatlabStatus= mexCallMATLAB(1,results,1,arguments,"single");
> >
> > Then
> >
> >
> > arguments[0] contains the mxArray with "double" data.
> >
> > results[0] contains the mxArray with "single" data.
> >
> > Then I have the following questions
> >
> >
> > 1) Can I delete ALL the data in arguments[0]. If I call:
> >
> > mxDestroyArray(arguments[0]) destroys all the data?. I am not sure, but I think
> > thay this destroys the mxArray object but not the memory allocated for containing all
> > the matrix.
>
> mxDestroyArray(arguments[0]) destroys the entire array including the data. i.e., it frees the memory for the mxArray structure as well as all the memory in the pr and pi data areas.
>
> > 2) I have the same question for the results[0]. I am not sure if the results[0] mxArray and
> > the data (memory allocated) containing the matrix can be destroyed.
> >
> > If I call:
> >
> > myMatrix=mxGetpr(results[0));
> > mxDestroy(results[0]);
>
> (typo) mxDestroyArray(results[0]);
>
> > Them I can use myMatrix, but If I later want to delete the matrix
> >
> > mxFree(myMatrix), then MATLAB gives an error and it hangs
>
> Yes, it will hang because myMatrix no longer points to valid memory. Don't destroy the mxArray until you are done with it *and* done with the data in the pr and pi areas. Don't use mxFree on the data area of an mxArray.
>
> James Tursa



Thank you very much James

JP

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com