13
votes

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?

1
Have You filled a bug report?Kamiccolo
@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
I would say, don't use macOS Majove.m7913d
@m7913d *MojaveS.S. Anne
@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

1 Answers

0
votes

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.