Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: matlab/mex function doesn't free memory
Replies: 22   Last Post: Dec 10, 2012 1:49 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
James Tursa

Posts: 1,061
Registered: 10/25/07
Re: matlab/mex function doesn't free memory
Posted: Apr 10, 2009 3:35 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Praetorian <ashish.sadanandan@gmail.com> wrote in message <b70044a2-8b1c-4fbf-b475-122750fdd38f@a5g2000pre.googlegroups.com>...
>
> I'm not so sure you can't use the mxMalloc and mxFree from within a
> shared library. I modified the code so that I was calling the DLL's
> allocate function followed immediately by the deallocate function; I
> then called the mex file from the command line 1000 times and didn't
> get any seg faults. The reason I got one yesterday was because I
> allocated memory using the DLL in one call to the mex file and then
> tried to deallocate it during the next call. But my tests from
> yesterday show that the Matlab memory manager does garbage collection
> on the memory allocated by the DLL when the calling mex file exits. So
> the memory allocated in the first call had already been deallocated
> (by the memory manager) and I tried to explicitly deallocate it in the
> next call resulting in the segv.
>
> Ashish.


Ah, got it. Thanks! I put in a mexMakeMemoryPersistent call in the dll and now everything works ok. That puts the burdon of clearing the memory on the dll programmer, but that's really as it should be. I tested my example using both API calls from within a mex and from the MATLAB prompt using loadlibrary and calllib. Works in both cases, no memory leak and no memory corruption. The dll detects when it is being detached from MATLAB, so the programmer can put code in the dll to automatically clear the memory in that event.

James Tursa



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.