I have a simple mex function, which calls another C++ function from a library. I compile the source with
mex -cxx mymexfunction.cpp -I/some/include -L/some/lib -lmylib
The mylib library is dynamic (.so) and is linked itself against some other libraries (boost, OpenCV and some more).
The problem I am having is that once I have called the function mymexfunction once, it will not get updated when I recompile the source code. I have tried
clear
clear all
clear mex
clear functions
clear fun('mymexfunction')
munlock('mymexfunction')
unloadlibrary('mymexfunction')
...but nothing helps! I have to restart Matlab in order to be able to see the updated mexfunction. Even if I delete the compiled mex file and recompile, I still get the old version of the mex function (not on disk, but in memory).
All is fine if I don't link against mylib, but I have no idea what could be the culprit that prevents the update. The library is unfortunately too large and too interwoven to remove single modules one by one.
Are there some known conditions which could cause such problems?
Clarification:
I only update the content of the mex function, not the library.
Update:
It works under Ubuntu 11.04 with Matlab R2011a! I tried to reproduce the same environment on my OpenSUSE machine (R2011a, Boost 1.42, OpenCV 2.2 dynamically linked, ...) but still no luck. So I conclude that nothing is actually wrong with my library (otherwise it wouldn't work under Ubuntu) but it must be some collision of the dependencies and Matlab internal libraries. I officially give up. Praetorian and Amro, thank you for your help!
The mex
command automatically clears the mex function if it is currently loaded in memory. Are you sure your mex function is closing whatever handle it holds to the other library? If such a handle exists it might prevent the OS from unloading the mex file.
I've used the following set of commands to clear mex functions manually, and from my experience, using a full path to the mex file when calling clear
works. So give this a try and if it still doesn't get unloaded you might want to start looking at the code for loading and unloading the other library.
[~,f] = inmem( '-completenames' );
result = strfind( f, ['mymexfile' '.' mexext] );
result = f(cellfun( @isempty, result, 'UniformOutput', true ) == 0);
clear( result{:} )
Try running the inmem
command again after the above and see if your mex file is still listed.
Something that might help you with making sure the other library gets unloaded is maybe using an std::shared_ptr
to hold the handle to this library. Then, at the beginning of the mexFunction()
entry point load the library and stick the handle into the shared_ptr
. The shared_ptr
will also need to use a custom deleter to unload the library (on Windows the custom deleter would call FreeLibrary
).
Of course, if this is being caused by a bug in the other library none of this is going to help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With