Search code examples
macosmatlabcrashcommand-line-interface

Matlab command line - some programs crash on exit


Certain Matlab scripts crash when I try to exit them from the command line, or when I put an "exit force" in the script. (The weird thing is I have not been able to determine what causes some programs to crash and some not to.) So e.g. here is a very simple Matlab program (bugtest.m) which exhibits this behaviour, on Mac OS:

function bugtest(ifile, ofile)
    data = csvread(ifile, 1, 0); % skip the first line
    csvwrite(ofile, data);
end

When I Matlab this script from the command line, and then type exit when I get the Matlab prompt, it works fine:

bash> /Applications/MATLAB_R2018b.app/bin/matlab -nodisplay -nojvm -r "bugtest('z2.csv','z3.csv')"

[Matlab copyright message]

>> exit
exit

But when I include the exit on the command line, it crashes (depending on the script, but the script bugtest.m always crashes):

bash> /Applications/MATLAB_R2018b.app/bin/matlab -nodisplay -nojvm -r "bugtest('z2.csv','z3.csv');exit"

[Matlab copyright message]

--------------------------------------------------------------------------------
       Segmentation violation detected at Thu Aug 22 15:55:40 2019 +0930
--------------------------------------------------------------------------------

Configuration:
  Crash Decoding           : Disabled - No sandbox or build area path
  Crash Mode               : continue (default)
[etc]

The same thing happens if there is an "exit force" inside bugtest.m. And yet other Matlab scripts work fine from the command line.

What is the cause of this problem, and how do I fix it?


Solution

  • To me this looks like a timing issue, where one thread is still finalizing writing to the file while another thread starts to tear down the runtime. I say this because when manually typing exit, some time has passed after running csvwrite, and the error doesn't occur.

    One can simulate this situation in a script by adding a small pause, for example pause(1), before calling exit.

    Obviously this is a bug that should be reported to the MathWorks so they can fix it.