MATLAB: Does MATLAB R2016b crash when launched from MATLAB-emacs

crashemacsMATLABshell_stack_size

When launched from within Emacs on Mac, MATLAB R2016b displays the following error and crashes:
objc[57745]: Class JavaLaunchHelper is implemented in both /Applications/MATLAB_R2016b.app/sys/java/jre/maci64/jre/bin/java (0x10cc664c0) and /Applications/MATLAB_R2016b.app/sys/java/jre/maci64/jre/lib/jli/libjli.dylib (0x11ef5f480). One of the two will be used. Which one is undefined.
This happens when MATLAB R2016b is launched through the terminal interface or MATLAB-emacs mode. MATLAB R2016b launches as expected from the system Terminal and the MATLAB icon in the Applications folder.
MATLAB R2016a and previous releases launch under Emacs without failure.
On Mac OS 10.12 (Sierra), the following stack trace is generated in the MATLAB crash log: ERROR: Stack Trace (from fault): [ 0] 0x0000000109377d04 /Applications/MATLAB_R2016b.app/bin/maci64/libmwfl.dylib+00036100 ZN2fl4diag15stacktrace_base7captureERKNS0_14thread_contextEm+00000052 [ 1] 0x000000010937af9a /Applications/MATLAB_R2016b.app/bin/maci64/libmwfl.dylib+00049050 _ZN2fl4test17terminate_handledEv+00000810 [ 2] 0x000000010937aa09 /Applications/MATLAB_R2016b.app/bin/maci64/libmwfl.dylib+00047625 _ZN2fl4diag13terminate_logEPKcPK17_darwin_ucontext+00000185 [ 3] 0x000000010c2b4ba8 /Applications/MATLAB_R2016b.app/bin/maci64/libmwmcr.dylib+00420776 Z32mnRunPathDependentInitializationN6mlutil10contextmgr5McrIDE+00003016 [ 4] 0x000000010c2b44e0 /Applications/MATLAB_R2016b.app/bin/maci64/libmwmcr.dylib+00419040 _Z32mnRunPathDependentInitializationN6mlutil10contextmgr5McrIDE+00001280 [ 5] 0x000000010c2b2f7a /Applications/MATLAB_R2016b.app/bin/maci64/libmwmcr.dylib+00413562 mnFatalSignalHandler+00000154 [ 6] 0x00007fffead15bba /usr/lib/system/libsystem_platform.dylib+00011194 _sigtramp+00000026 [ 7] 0x000000010c0b744a /Applications/MATLAB_R2016b.app/bin/maci64/libmwiqm.dylib+00185418 _ZN5boost4bindIbN3iqm22IntermediatePacketInfoERKNSt3_112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEENS_3argILi1EEES9_EENS_3_bi6bind_tIT_NS_4_mfi4cmf1ISG_T0_T1_EENSE_9list_av_2IT2_T3_E4typeEEEMSJ_KFSG_SK_ESN_SO_+00002378 [ 8] 0x000000010c259467 /Applications/MATLAB_R2016b.app/bin/maci64/libmwmcr.dylib+00046183 ZN5boost11unique_lockINS_5mutexEE4lockEv+00000039 [ 9] 0x000000010c25a543 /Applications/MATLAB_R2016b.app/bin/maci64/libmwmcr.dylib+00050499 _ZN5boost18condition_variable4waitERNS_11unique_lockINS_5mutexEEE+00000083 [ 10] 0x000000010c2593db /Applications/MATLAB_R2016b.app/bin/maci64/libmwmcr.dylib+00046043 _ZN5boost6detail17shared_state_base13wait_internalERNS_11unique_lockINS_5mutexEEEb+00000091 [ 11] 0x00007fffeac8c9d1 /usr/lib/system/libsystem_malloc.dylib+00014801 tiny_malloc_from_free_list+00000431 [ 12] 0x00007fffeac8b522 /usr/lib/system/libsystem_malloc.dylib+00009506 szone_malloc_should_clear+00000400 [ 13] 0x00007fffeac8b332 /usr/lib/system/libsystem_malloc.dylib+00009010 malloc_zone_malloc+00000107 [ 14] 0x00007fffeac8a2b0 /usr/lib/system/libsystem_malloc.dylib+00004784 malloc+00000024 [ 15] 0x00000001093d97d3 /Applications/MATLAB_R2016b.app/bin/maci64/libmwfl.dylib+00436179 _ZN2fl3mem6detail24default_vector_alignmentEv+00011603 [ 16] 0x000000010953842c /Applications/MATLAB_R2016b.app/bin/maci64/libmx.dylib+00005164 _ZN6matrix6detail10noninlined12mx_array_api15mxMallocExCheckEm+00000044 [ 17] 0x000000010cc3d210 /Applications/MATLAB_R2016b.app/bin/maci64/libmwlxetypes.dylib+00061968 _ZN9MathWorks3lxe22MxArrayToXvalueTypeMap24GetXvalueTypeFromMxArrayERNS_2ts12iTypeFactoryEPK11mxArray_tag+00000048 [ 18] 0x000000010cc45c87 /Applications/MATLAB_R2016b.app/bin/maci64/libmwlxetypes.dylib+00097415 _ZN9MathWorks3lxe6xvalue19InitializeToMxArrayENSt3_110unique_ptrI11mxArray_tagN6matrix6detail17mxDestroy_deleterEEE+00000071 [ 19] 0x000000010cc45dbd /Applications/MATLAB_R2016b.app/bin/maci64/libmwlxetypes.dylib+00097725 ZN9MathWorks3lxe6xvalueC2ENSt3_110unique_ptrI11mxArray_tagN6matrix6detail17mxDestroy_deleterEEE+00000029 [ 20] 0x000000010dfa9885 /Applications/MATLAB_R2016b.app/bin/maci64/libmwm_lxe.dylib+10565765 _ZN9MathWorks3lxe14AllocateOutputIZNS0_21AllocateOutputForTempIbLb0EEEvlPNS0_24ElementwiseOperandLayoutEmPmPPPvPbbEUlNS_2ts4TypeEE_EEvPNS0_6xvalueES4_lT_mPKm9mxClassIDb+00000517 [ 21] 0x000000010df7ef87 /Applications/MATLAB_R2016b.app/bin/maci64/libmwm_lxe.dylib+10391431 _ZN9MathWorks3lxe29elem_expression_check_anyTypeIbLb0EEEvPv+00000919
On Mac OS 10.11 (El Capitan) and Mac OS 10.10 (Yosemite), the following stack trace is generated in the MATLAB crash log: ERROR: Stack Trace (captured): [ 0] 0x000000010e141cb4 /Applications/MATLAB_R2016b.app/bin/maci64/libmwfl.dylib+00036020 ZN2fl4diag15stacktrace_base7captureEm+00000020 [ 1] 0x000000010e14506d /Applications/MATLAB_R2016b.app/bin/maci64/libmwfl.dylib+00049261 _ZN2fl4test17terminate_handledEv+00001021 [ 2] 0x000000010e144a09 /Applications/MATLAB_R2016b.app/bin/maci64/libmwfl.dylib+00047625 _ZN2fl4diag13terminate_logEPKcPK17_darwin_ucontext+00000185 [ 3] 0x0000000111a2bba8 /Applications/MATLAB_R2016b.app/bin/maci64/libmwmcr.dylib+00420776 Z32mnRunPathDependentInitializationN6mlutil10contextmgr5McrIDE+00003016 [ 4] 0x0000000111a2b4e0 /Applications/MATLAB_R2016b.app/bin/maci64/libmwmcr.dylib+00419040 _Z32mnRunPathDependentInitializationN6mlutil10contextmgr5McrIDE+00001280 [ 5] 0x0000000111a2c45a /Applications/MATLAB_R2016b.app/bin/maci64/libmwmcr.dylib+00423002 _Z32mnRunPathDependentInitializationN6mlutil10contextmgr5McrIDE+00005242 [ 6] 0x0000000111a2af01 /Applications/MATLAB_R2016b.app/bin/maci64/libmwmcr.dylib+00417537 _Z22mnInstallCrashHandlersv+00000497 [ 7] 0x00007fff915180a1 /usr/lib/libc++abi.dylib+00155809 _ZSt11_terminatePFvvE+00000008 [ 8] 0x00007fff91518113 /usr/lib/libc++abi.dylib+00155923 _ZSt9terminatev+00000051 [ 9] 0x00000001119d1593 /Applications/MATLAB_R2016b.app/bin/maci64/libmwmcr.dylib+00050579 _ZN5boost18condition_variable4waitERNS_11unique_lockINS_5mutexEEE+00000163 [ 10] 0x00000001119d03db /Applications/MATLAB_R2016b.app/bin/maci64/libmwmcr.dylib+00046043 _ZN5boost6detail17shared_state_base13wait_internalERNS_11unique_lockINS_5mutexEEEb+00000091 [ 11] 0x0000000000000004 <unknown-module>+00000000

Best Answer

The error you are seeing, and the definition of JavaLaunchServer, is not the root cause of the crash.
The cause of the crash is that Mac requires the shell's stack size to be a multiple of 4KB, but the emacs shell uses a nonstandard default stack size. MATLAB does not correct this when launched from inside emacs, and this then leads to this error.
A workaround for this is to set the default stack size within the emacs shell that launches MATLAB to a multiple of 4K:
1. create a file named matlab_emacs_wrapper with the following content:
 
#!/bin/bash
shell_stack_size=$(ulimit -s)
  1. round stack size to next 4K boundary:
adjusted_stack_size=$[4*((($shell_stack_size-1)/4)+1)]
ulimit -s $adjusted_stack_size && matlab $@
 
2. add the following to your .emacs file, after loading matlab-emacs:
 
(customize-set-variable 'matlab-shell-command "matlab_emacs_wrapper")
After that, running matlab-shell and other commands from inside matlab-emacs should get the correct stack size.
However, if you are launching MATLAB from emacs directly, please launch matlab_emacs_wrapper instead.
Related Question