Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Destroy Array
Date: Wed, 24 Aug 2011 14:40:28 +0000 (UTC)
Organization: Boeing Co
Lines: 30
Message-ID: <j332gs$8f2$1@newscl01ah.mathworks.com>
References: <j2tidu$2p4$1@newscl01ah.mathworks.com> <j2tj0l$4qq$1@newscl01ah.mathworks.com> <j2tpgo$s86$1@newscl01ah.mathworks.com> <j32vin$rcr$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-01-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1314196828 8674 172.30.248.46 (24 Aug 2011 14:40:28 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 24 Aug 2011 14:40:28 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:741125

"Stiphu" wrote in message <j32vin$rcr$1@newscl01ah.mathworks.com>...
> 
> An excerpt from the Matlab example "mexgettaray.c":
> 
> 	/* Since variable does not yet exist in MATLAB workspace,
>            create it and place it in the global workspace. */
> 	array_ptr=mxCreateDoubleMatrix(1,1,mxREAL);
>     }
>     
>     /* Increment both MATLAB and MEX counters by 1 */
>     mxGetPr(array_ptr)[0]+=1;
>     mex_count=(int)mxGetPr(array_ptr)[0];
>     mexPrintf("%s has been called %i time(s)\n", mexFunctionName(), mex_count);
>     
>     /* Put variable in MATLAB global workspace */
>     status=mexPutVariable("global", array_name, array_ptr);
>     
>     if (status==1){
> 	mexPrintf("Variable %s\n", array_name);
> 	mexErrMsgTxt("Could not put variable in global workspace.\n");
>     }
>     
>     /* Destroy array */
>     mxDestroyArray(array_ptr);
> 
> There, they don't mention the two lines you added to my code. Why?

Because they are not attaching other memory to the mxArray. Note that in the above example they create a variable with mxCreateDoubleMatrix, then access its data area directly with the mxGetPr call.  Since they never try to *replace* the data pointer via a mxSetPr call (like you do) there is no memory leak and there is nothing that needs to get detached before the mxArray gets destroyed.

James Tursa