Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Release dll files loaded within mex file

429 views
Skip to first unread message

Alexander

unread,
Oct 1, 2009, 11:00:19 AM10/1/09
to
Hi everybody,

I have the following problem, which I'm hoping someone here can help me with.

I have a C++ mex file which is linked against a dynamic library. Everything compiles and links fine and the function is doing what it's supposed to be doing. However, when I call that mex function for the first time, Matlab locks the .dll file and does not release it until I quit Matlab (e.g. I can't delete or overwrite the file while Matlab is running). This is annoying because I want to be able to recompile and test the dynamic library without having to quit Matlab every time. How do I tell Matlab to release/clear/unload that dll?

Thanks a lot for any help!
Alexander Ecker

Ashish Uthama

unread,
Oct 1, 2009, 11:11:27 AM10/1/09
to

try 'clear mex'

Alexander

unread,
Oct 1, 2009, 12:48:02 PM10/1/09
to
> try 'clear mex'

I tried that but it only clears the mex file itself but not the libraries it loads.

Alex

John Reilly

unread,
Oct 1, 2009, 12:58:18 PM10/1/09
to
how about "clear functions"?

-reilly.

Alexander

unread,
Oct 1, 2009, 1:09:18 PM10/1/09
to
> how about "clear functions"?

Nope. Neither "clear", "clear all", "clear mex" nor "clear functions" works.

Alex

Ashish Uthama

unread,
Oct 1, 2009, 1:22:14 PM10/1/09
to

Once you do a clear mex, the mex file is off the hook, which I think,
should mean that the dependent DLL is also free from MATLAB.
Is anything that the mex file does with DLL cause it to directly get tied
to the MATLAB process (instead of just through the mex file)?


Praetorian

unread,
Oct 1, 2009, 3:08:25 PM10/1/09
to
On Oct 1, 11:22 am, "Ashish Uthama" <first.l...@mathworks.com> wrote:
> On Thu, 01 Oct 2009 13:09:18 -0400, Alexander  
>
> <alexander.ec...@remove.this.tuebingen.mpg.de> wrote:
> >> how about "clear functions"?
>
> > Nope. Neither "clear", "clear all", "clear mex" nor "clear functions"  
> > works.
>
> > Alex
>
> Once you do a clear mex, the mex file is off the hook, which I think,  
> should mean that the dependent DLL is also free from MATLAB.
> Is anything that the mex file does with DLL cause it to directly get tied  
> to the MATLAB process (instead of just through the mex file)?

I would say the OP needs to call FreeLibrary (on Windows) or its
equivalent from within the MEX file when he's done using the DLL
instead of relying on Matlab to unload the dependent DLL. I suspect
Matlab itself has no way of doing this since it isn't even aware the
MEX file is loading a separate DLL and when the OP closes Matlab it is
actually the OS that is unloading the DLL as part of clearing up the
application's memory space.

HTH,
Ashish.

James Tursa

unread,
Oct 1, 2009, 5:53:01 PM10/1/09
to
Praetorian <ashish.s...@gmail.com> wrote in message <d55d55f4-5cc3-43cd...@c28g2000yqd.googlegroups.com>...

>
> I would say the OP needs to call FreeLibrary (on Windows) or its
> equivalent from within the MEX file when he's done using the DLL
> instead of relying on Matlab to unload the dependent DLL.

I don't know if OP has this option. From his description, he links in the dl during the compiling process rather than calling LoadLibrary at run-time to get a handle to the loaded dll. Bottom line is that OP has no handle to the loaded dll so he can't call FreeLibrary.

James Tursa

Alexander

unread,
Oct 2, 2009, 9:28:04 AM10/2/09
to
> I don't know if OP has this option. From his description, he links in the dl during the
> compiling process rather than calling LoadLibrary at run-time to get a handle to the
> loaded dll. Bottom line is that OP has no handle to the loaded dll so he can't call
> FreeLibrary.

It's actually both. Some libraries are linked in at compile time, others are loaded dynamically at run time. The problem occurs with both. Unfortunately I haven't worked a whole lot with dynamic libraries, so I don't really know what I'm searching for.

I'm using Qt's PluginLoader to load the libraries at run time. According to the documentation, those libraries should be unloaded when the program exits. Unfortunately this doesn't seem to happen. Also, the Qt libraries, which are linked in at compile time, aren't unloaded when my mex file exits or is cleared.

If I understand you correctly I cannot free the library explicitly from my mex file. Does that mean there is no way of freeing the library other than closing Matlab? That seems somewhat odd.

Best,
Alex

Praetorian

unread,
Oct 2, 2009, 11:21:03 AM10/2/09
to
On Oct 2, 7:28 am, "Alexander "

Doesn't the Qt PluginLoader class have any function that allows you to
explicitly unload the libraries you've loaded? Or does it just rely on
the destructor to perform cleanup? Even in the latter case it is odd
that the unloading doesn't happen when your mex file exits. Is your
PluginLoader class instance a global variable within the mex file? If
that's the case the destructor will not be called until Matlab clears
the mex file, even so, 'clear <mexFileName>' should release the
libraries. I've never programmed with Qt so maybe this is a quirk that
comes along with it, although that seems unlikely to me.

As far as libraries linked in at compile time go, as I understand
this, all the linker does is resolve addresses at link time. The
actual DLL does still need to be loaded at runtime. I could be wrong
about this; James can probably set us straight on this subject. But if
that is the case then it would seem likely that there is also
provision to unload these automatically loaded DLLs when the caller
get unloaded.

Anyway, you might have better luck posting this on a Qt newsgroup. You
probably already know this, but on Windows mex files are just regular
DLLs with a predefined (mexFunction()) entry point; that might help
you in explaining the problem on another newsgroup.

HTH,
Ashish.

Francisco

unread,
Sep 2, 2010, 11:23:22 AM9/2/10
to
Praetorian <ashish.s...@gmail.com> wrote in message <46f6a0bb-4f15-42f7...@t2g2000yqn.googlegroups.com>...

I have the same problem for a couple of days now and i cannot figure it out. None of the clear functions work. Has anybody figure this one out?

John Sadowsky

unread,
May 14, 2014, 7:33:13 PM5/14/14
to
>
> I have the same problem for a couple of days now and i cannot figure it out. None of the clear functions work. Has anybody figure this one out?

Several years later ... is there an answer to this question?

Steven Lord

unread,
May 14, 2014, 10:51:25 PM5/14/14
to

"John Sadowsky" <sado...@broadcom.com> wrote in message
news:ll0ufp$ps6$1...@newscl01ah.mathworks.com...
>>
>> I have the same problem for a couple of days now and i cannot figure it
>> out. None of the clear functions work. Has anybody figure this one out?
>
> Several years later ... is there an answer to this question?

Did you try what was suggested in message 10 in the thread?

If you did and it didn't work, either tell the group in more detail what you
tried and/or contact Technical Support.

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Enrique Guerrero

unread,
Aug 26, 2014, 3:22:06 AM8/26/14
to
"John Sadowsky" <sado...@broadcom.com> wrote in message <ll0ufp$ps6$1...@newscl01ah.mathworks.com>...
> >
> > I have the same problem for a couple of days now and i cannot figure it out. None of the clear functions work. Has anybody figure this one out?
>
> Several years later ... is there an answer to this question?

I think I have something that could help. Take a look at
help loadlibrary
help libisloaded
help unloadlibrary

If you have loaded the DLL with a certain alias, you can use the latter with the unloadlibrary function in order to release it.

I hope this works!

Stepan -

unread,
Apr 3, 2015, 10:08:16 AM4/3/15
to
"Enrique Guerrero" wrote in message <lthciu$q4h$1...@newscl01ah.mathworks.com>...
> "John Sadowsky" <sado...@broadcom.com> wrote in message <ll0ufp$ps6$1...@newscl01ah.mathworks.com>...
> > >
> > > I have the same problem for a couple of days now and i cannot figure it out. None of the clear functions work. Has anybody figure this one out?
> >
> > Several years later ... is there an answer to this question?
>

A mex won't unload if it is holding on to specific system resources. Threads definitely, perhaps others. It does not matter whether the threads are created in your mex implementation or in a library you loaded.

For example if you are using OpenCV with multithreading support (OpenMP, TBB, Concurrency), it automatically creates a pool of worker threads that keep running even after the mex function finishes. You need to stop these threads before calling clear mex.

Since it is easier to debug a singlethreaded implementation anyway, I am calling
cv :: setNumThreads (0); as the very first thing in my mexFunction. This way the helper threads are not created at all. And the mex can be unloaded with 'clear mex'.

Hope this helps.
I am not sure about Qt, but I would assume the situation is similar. If there is a function to close/tear down the library, try calling it before invoking clear mex.
0 new messages