9

I have a Mex-function, say myfunction.mexmaci64 (which is the correct ending on OS X).

Now, myfunction is linked against a library mylibrary.dylib. Both, mex-file and library, reside in the same folder.

Now, whenever I change something in mylibrary, MATLAB does not reload the new library version but instead uses the old one until I do restart MATLAB. That is very anoing when doing development and debugging work. Is there a way to force MATLAB to reload the library without restarting the application?

Note: It would be easy to link the library statically into the mex function. However, as I link the same library across quite a few mex-files, I would prefer to keep my single shared library to reduce compilation times and data redundancy.

Edit:

Concerning the discussion wether the clear mex helps:

[~, loaded_mexes] = inmem('-completenames'); % get canonica

returns a list with all loaded mex-files. This list does not contain the linked library, but only the mex-files itself. Using clear mex successfully empties this list, but does not unload mylibrary - running the mex function again still yields the same output as it did with the old shared library.

Thilo
  • 8,827
  • 2
  • 35
  • 56
  • Would `unloadlibrary()` be an option? http://www.mathworks.de/de/help/matlab/ref/unloadlibrary.html – H.Muster Jun 25 '13 at 12:58
  • @H.Muster Maybe `unloadlibrary()` could help, yes. However, I do not see how to use it. Calling it just with the path of my library just returns `Could not find file mylibrary.dylib`. Furthermore, the documentation also lists the limitation `Use with libraries that are loaded using the loadlibrary function.`. Probably the mex-file does not use `loadlibrary` (I never provide a header-file required for `loadlibrary`. – Thilo Jun 25 '13 at 13:22
  • 1
    @Thilo I've only had to deal with unloading the mex function itself, not libraries that it references. I assumed the OS would unload it for you once MATLAB unloaded the mex file. How are you linking to `mylibrary`? Is the linker doing the binding for you (when you compile the mex function), or are you using `dlopen()`? If it's the latter, you need to call `dlclose()`. Maybe register a `mexAtExit` function where you can make this call. – Praetorian Jun 25 '13 at 15:27
  • @Thilo: `inmem` only shows MEX-functions loaded in memory not external dependencies. On Windows, you could use `feature dumpmem` to see all DLLs loaded by MATLAB (including its own) – Amro Jun 25 '13 at 16:27
  • possible duplicate of [Mex function not updated after recompile](http://stackoverflow.com/questions/7012408/mex-function-not-updated-after-recompile). Last time I investigated this, I was not able to reproduce the problem. The conclusion was that the shared library linked did not release all its resources, thus remained in memory (or that this behavior is platform-specific, I tested it on Windows) – Amro Jun 25 '13 at 16:33
  • @Amro: Thank you for your input. It is quite likely that the library does not release every bit of memory allocated. `bdclose all` works for now. Once I have a bit more time I will investigate a bit further, though. Maybe a problem with my library is involved (that in the worst case might be the cause of other problems I experience when I use the library outside of MATLAB). – Thilo Jun 25 '13 at 17:52

3 Answers3

5

To clear a library from memory I usually have excellent luck with

bdclose all;

Then if I'm really feeling militant, I'll do:

bdclose all; % clear all libraries out of memory ( supposedly )
clear all;   % clear all workspace variables, mex, etc. ( supposedly )
rehash;      % cause all .m files to be reparsed when invoked again
macduff
  • 4,655
  • 18
  • 29
  • Thats unsual, `bdclose` is for closing Simulink windows, nothing to do with MEX-files (at least not documented).. – Amro Jun 25 '13 at 16:28
  • For me, `bdclose all;` just seems to take a while starting simulink, but does not help reload the mex. – Alec Jacobson Jun 10 '18 at 16:17
1

Does clear mex do what you need?

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
  • @Thilo `clear` is the right answer, check the documentation for the right syntax. Try `clear abs-path-to-mex-file`. If nothing works, dig through the m-file for the `mex` command to see how Matlab unloads mex files in memory when you recompile one. – Praetorian Jun 25 '13 at 12:56
  • @Praetorian: `clear mex` works, but does *not* clear the shared library, but only the mex-file. In fact, I could find no trace in the `mex.m` (or better, `mex_helper.m`) that linked libraries are reloaded (searching for `unload` or `lib` does not yield a significant hit). – Thilo Jun 25 '13 at 13:16
0

You can see what shared libraries are loaded by doing:

version('-modules')

I had success with unloading a mex file and (the shared library it depended on) by doing

version('-modules')  % test.mexa64 and test.so appear
clear test           % clear the mex file
version('-modules')  % both test.mexa64 and test.so no longer appear.
m7913d
  • 10,244
  • 7
  • 28
  • 56
codehippo
  • 1,379
  • 1
  • 8
  • 19