Main Content

MEX Generated on macOS Platform Stays Loaded in Memory

Issue

When generating MEX code on the macOS platform, you get one of these messages:

  • Warning message:

    The generated code contains usage of OpenMP thread private variable.
    This can cause the MEX to remain loaded in the memory.
  • Error message:

    The MEX file 'foo_mex' is still loaded in memory. 
    To clear the MEX file from memory, close the MATLAB session.

Cause

Your MATLAB® code contains global or persistent variables that are reachable from the body of a parfor-loop. Here is an example MATLAB function that contains this code pattern.

function y = foo(x)
y = coder.nullcopy(x);
parfor i = 1:numel(x)
    y(i) = x(i) + sub;
end

function y = sub
persistent t;
if isempty(t)
    t = 2;
end
y = t;

When you generate a MEX function for foo for the first time, you can receive the warning message.

If you try to overwrite the generated MEX by generating code for foo again, you can receive the error message.

Solution

If you receive the warning message, you can use the generated MEX function.

If you receive the error message, close the current MATLAB session to clear the MEX function foo_mex from memory. To overwrite the previously generated MEX function , open a new MATLAB session and generate MEX code for foo.

See Also

Related Topics