16

In MATLAB, clear mex unloads all MEX-files from memory (unless they're locked). Under previous versions of macOS, I was able to re-compile a MEX-file and run the modified version without restarting MATLAB, simply by issuing a clear mex command. This is no longer possible under Mojave.

For example, take this trivial MEX-file (get_data_pointer.c):

#include "mex.h"

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
  plhs[0] = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
  *(uint64_t*)mxGetData(plhs[0]) = (uint64_t)mxGetData(prhs[0]);
}

We can create the MEX-file and load it in memory with

mex get_data_pointer.c
get_data_pointer(0)

To clear it,

clear mex
[~,mexfiles] = inmem
version -modules

inmem indeed returns an empty cell array indicating no MEX-files are loaded in memory, But version -modules (undocumented, from this answer) still shows /Users/cris/matlab/get_data_pointer.mexmaci64 in its output. And changing the MEX-file source code and re-compiling demonstrates that, indeed, the MEX-file is never reloaded, the old version is still being run until one exits MATLAB.

I am seeing this on MATLAB R2017a on macOS Mojave. This was never a problem with the same MATLAB version under High Sierra.

How can I force MATLAB to unload the MEX-file without restarting?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • Have You filled a bug report? – Kamiccolo Nov 15 '19 at 08:04
  • @Kamiccolo: No, I haven’t. I have the impression that this is an issue with the OS, but I’m not sure, it could be MATLAB as well. So I was hoping to learn more about it before filing a bug report. – Cris Luengo Nov 15 '19 at 13:42
  • Well, I doubt Apple is going to fix something which is not working on MatLab ;) Sadly, there is not much usable information to debug this problem in Your original question. Have You tried running minimal possible Mex file and tracing library calls related to loading/unloading and relevant errors? – Kamiccolo Nov 17 '19 at 21:45
  • @Kamiccolo: I figure this is either an intended change in macOS, and MATLAB needs to fix the issue on their end, or it is an unintended change in macOS, and Apple needs to fix it on their end. Either way, there might be a way to purge from memory dynamic link libraries that are no longer linked to. This is what I'm looking for. – Cris Luengo Nov 18 '19 at 01:38
  • 3
    I would say, don't use macOS Majove. – m7913d Nov 20 '19 at 22:08
  • 2
    @m7913d *Mojave – S.S. Anne Nov 21 '19 at 12:33
  • 3
    @CrisLuengo: Even if it's the OS which might cause the problem, that is something Mathworks has to investigate. They have MacOS, i doubt apple has MATLAB available. I would Mathworks in such a situation. – Daniel Nov 21 '19 at 19:58
  • Were you ever able to resolve this problem? I am also facing a similar issue where the mex file compiled outside doesnt get "unloaded from memory" So I can only ever run the it once. I have to exit matlab entirely in order to run it again. – Omi Mar 22 '22 at 19:42
  • @Omi: No, I never did. I've accepted I need to restart MATLAB when rebuilding MEX-files. :( – Cris Luengo Mar 22 '22 at 19:52

1 Answers1

-1

That's probably the libstdc++ change. This is a runtime library which Apple deprecated quite a while ago (XCode 8 I think) and finally dropped completely in XCode 10 and Mojave. So the MEX file you have was probably compiled with an older version.

The MathWorks rules on MEX file compatibility is that they will often work between versions, but if there is an incompatible change (like this one), then you need to recompile.

MPG
  • 835
  • 4
  • 10
  • As you can tell from the question text, the problem happens with MEX-files compiled on the same version of MATLAB and same version of the OS. I actually demonstrate the problem with a simple MEX-file that I compiled, ran, and then re-compiled without exiting MATLAB. – Cris Luengo Nov 21 '19 at 20:37