MATLAB mex file with boost (e.g. when using CGAL)

Alec Jacobson

December 07, 2012

weblog/

On my laptop I have MATLAB2011b and I was able to compile a mex function using CGAL (and by dependency boost), just fine. When I tried this code on my iMac at work, the code compiled fine, but at runtime matlab crashed somewhere deep inside Cgal and boost:
[  0] 0x00000001000e4e46 /Applications/MATLAB_R2012b.app/bin/maci64/libmwfl.dylib+00036422 _ZN2fl4diag15stacktrace_base7captureERKNS0_14thread_contextEm+000150
[  1] 0x00000001000e5af1 /Applications/MATLAB_R2012b.app/bin/maci64/libmwfl.dylib+00039665 fl_diag_terminate+000321
[  2] 0x00000001000e7c34 /Applications/MATLAB_R2012b.app/bin/maci64/libmwfl.dylib+00048180 _ZN2fl4diag13terminate_logEPKcRKNS0_14thread_contextE+000100
[  3] 0x00000001008e125e /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00315998 mnTrapCtrlc+000254
[  4] 0x00000001008e2d24 /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00322852 _Z32mnRunPathDependentInitializationv+003492
[  5] 0x00000001008e313d /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00323901 _Z32mnRunPathDependentInitializationv+004541
[  6] 0x00000001008e37f5 /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00325621 _Z32mnRunPathDependentInitializationv+006261
[  7] 0x00000001008e3aa1 /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00326305 mnSIGABRTHandler+000129
[  8] 0x00007fff90d64cfa                  /usr/lib/system/libsystem_c.dylib+00666874 _sigtramp+000026
[  9] 0x00007fff6332b6f8                  /usr/lib/system/libsystem_c.dylib+18446744072944523000
[ 10] 0x00007fff90d03a7a                  /usr/lib/system/libsystem_c.dylib+00268922 abort+000143
[ 11] 0x0000000121634c3e              /opt/local/lib/gcc43/libgcc_s.1.dylib+00060478 uw_init_context_1+000142
[ 12] 0x0000000121635058              /opt/local/lib/gcc43/libgcc_s.1.dylib+00061528 _Unwind_Resume+000072
[ 13] 0x000000012106e2da /Users/ajx/Documents/volume/matlab/peeling/peeling.mexmaci64+00045786 mexFunction+001450
[ 14] 0x000000010550deca /Applications/MATLAB_R2012b.app/bin/maci64/libmex.dylib+00065226 mexRunMexFile+000090
[ 15] 0x0000000105509a29 /Applications/MATLAB_R2012b.app/bin/maci64/libmex.dylib+00047657 _ZN7Mfh_mex30runMexFileWithSignalProtectionEiPP11mxArray_tagiS2_+000137
[ 16] 0x000000010550b038 /Applications/MATLAB_R2012b.app/bin/maci64/libmex.dylib+00053304 _ZN7Mfh_mex13dispatch_fileEiPP11mxArray_tagiS2_+000232
[ 17] 0x00000001009dab43 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_dispatcher.dylib+00314179 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000499
[ 18] 0x0000000100d8b5b2 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03278258 _ZN20ResolverFunctionDesc12CallFunctionEiPP11mxArray_tagiS2_+000914
[ 19] 0x0000000100d9287f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03307647 _ZN8Resolver13CallMFunctionEiiP10_m_operandP17m_operand_storageiS1_S3_Pi+001679
[ 20] 0x0000000100d934e6 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03310822 _Z22inResolveMFunctionCallP16_m_function_desciiP10_m_operandP17m_operand_storageiS2_S4_PiP13inMarshalTypeiPK19mpsTypeSequenceNlhsPFP11mxArray_tagiE+000598
[ 21] 0x0000000100bc9b4f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01436495 _ZN9accelImpl13MFunctionCallEPP8_accelOp+000527
[ 22] 0x0000000100bf4b7e /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612670 _ZN9accelImpl4ExecEv+000286
[ 23] 0x0000000100bf4c3b /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612859 _ZNK9accelCode4CallEP13inMarshalTypePi+000091
[ 24] 0x0000000100d39f16 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+02944790 _ZN5inJit17ExecuteHotSegmentEP15_inJitAccelInfoP7opcodesPiPl+001654
[ 25] 0x0000000100b24361 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00758625 _Z16inEnterDebugModeN5boost8functionIFvvEEEb+012273
[ 26] 0x0000000100b29c1c /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00781340 _Z18protected_inInterp12inDebugCheckii7opcodesPV15inPcodeNest_tagPl+000140
[ 27] 0x0000000100b2614e /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00766286 _Z16inEnterDebugModeN5boost8functionIFvvEEEb+019934
[ 28] 0x0000000100b27630 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00771632 _Z26inExecuteMFunctionOrScriptP6Mfh_mpb+000688
[ 29] 0x0000000100b9482a /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01218602 _Z10inRunMfileiPP11mxArray_tagiS1_P6Mfh_mpP15inWorkSpace_tag+001530
[ 30] 0x00000001009dab43 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_dispatcher.dylib+00314179 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000499
[ 31] 0x0000000100b76a5a /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01096282 _Z19inDispatchFromStackiPKcii+001034
[ 32] 0x0000000100b02979 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00620921 inCallFcnFromReference+000265
[ 33] 0x0000000100b23125 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00753957 _Z16inEnterDebugModeN5boost8functionIFvvEEEb+007605
[ 34] 0x0000000100b29c1c /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00781340 _Z18protected_inInterp12inDebugCheckii7opcodesPV15inPcodeNest_tagPl+000140
[ 35] 0x0000000100b2614e /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00766286 _Z16inEnterDebugModeN5boost8functionIFvvEEEb+019934
[ 36] 0x0000000100b27630 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00771632 _Z26inExecuteMFunctionOrScriptP6Mfh_mpb+000688
[ 37] 0x0000000100b9482a /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01218602 _Z10inRunMfileiPP11mxArray_tagiS1_P6Mfh_mpP15inWorkSpace_tag+001530
[ 38] 0x00000001009dab43 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_dispatcher.dylib+00314179 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000499
[ 39] 0x0000000100b62904 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01014020 _Z23inEvalPcodeHeaderToWordP15_memory_contextiPP11mxArray_tagP12_pcodeheaderP6Mfh_mpj+000260
[ 40] 0x0000000100b1c162 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00725346 _Z25in_local_call_with_setjmpIN5boost3_bi6bind_tIvPFvP15_memory_contextPiPP11mxArray_tagP12_pcodeheaderjENS1_5list5INS1_5valueIS4_EENS0_3argILi1EEENSG_ILi2EEENSE_ISA_EENSE_IiEEEEEEE17inExecutionStatusT_S5_S8_b+000082
[ 41] 0x0000000100b187f5 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00710645 _ZN7EvalLog6setLogEiPP11mxArray_tagiS2_+003381
[ 42] 0x0000000100b18b75 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00711541 _ZN7EvalLog6setLogEiPP11mxArray_tagiS2_+004277
[ 43] 0x000000010640058e /Applications/MATLAB_R2012b.app/bin/maci64/libmwbridge.dylib+00058766 _Z28evalCommandWithLongjmpSafetyPKc+000094
[ 44] 0x0000000106400d8b /Applications/MATLAB_R2012b.app/bin/maci64/libmwbridge.dylib+00060811 _Z28evalCommandWithLongjmpSafetyRKSbItSt11char_traitsItESaItEE+000043
[ 45] 0x000000010640181d /Applications/MATLAB_R2012b.app/bin/maci64/libmwbridge.dylib+00063517 _Z8mnParserv+001021
[ 46] 0x00000001008c4139 /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00196921 _ZN11mcrInstance30mnParser_on_interpreter_threadEv+000041
[ 47] 0x000000010089e76b /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00042859 _ZN3mcr7runtime17InterpreterThread4Impl17InvocationRequest3runEv+000523
[ 48] 0x000000010089e925 /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00043301 _ZN3mcr7runtime17InterpreterThread4Impl26invocation_request_handlerEl+000053
[ 49] 0x000000010042f574 /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00107892 _ZN10eventqueue18UserEventQueueImpl5flushEv+000756
[ 50] 0x000000010042f79a /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00108442 _ZN10eventqueue8ReadPipeEib+000026
[ 51] 0x000000010042df26 /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00102182 _ZN10eventqueue18UserEventQueueImpl9selectFcnEb+000326
[ 52] 0x0000000107d26a9d /Applications/MATLAB_R2012b.app/bin/maci64/libmwuix.dylib+00170653 _ZN21uix_nodisplay_ppeHook16pollingDuringFcnEb+000045
[ 53] 0x00000001004d71b2 /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00795058 _ZSt8for_eachIN9__gnu_cxx17__normal_iteratorIPN5boost8weak_ptrIN4sysq10ws_ppeHookEEESt6vectorIS6_SaIS6_EEEENS4_8during_FIS6_NS2_10shared_ptrIS5_EEEEET0_T_SH_SG_+000162
[ 54] 0x00000001004d8dab /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00802219 _ZN4sysq12ppe_for_eachINS_8during_FIN5boost8weak_ptrINS_10ws_ppeHookEEENS2_10shared_ptrIS4_EEEEEET_RKS9_+000203
[ 55] 0x00000001004d592c /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00788780 _ZN4sysq19ppePollingDuringFcnEb+000092
[ 56] 0x00000001004d5ac7 /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00789191 _ZN4sysq11ppeMainLoopEiib+000119
[ 57] 0x00000001004d5c94 /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00789652 _ZN4sysq11ppeLoopIfOKEiib+000132
[ 58] 0x00000001004d5e34 /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00790068 _ZN4sysq20processPendingEventsEiib+000132
[ 59] 0x000000010089ea9f /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00043679 _ZN3mcr7runtime17InterpreterThread4Impl14process_eventsERKN5boost10shared_ptrIS2_EE+000223
[ 60] 0x000000010089f2ca /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00045770 _ZN3mcr7runtime17InterpreterThread4Impl3runERKN5boost10shared_ptrIS2_EEPNS2_12init_contextE+000282
[ 61] 0x0000000100897176 /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00012662 _Z26run_init_and_handle_eventsPv+000054
[ 62] 0x00007fff90d108bf                  /usr/lib/system/libsystem_c.dylib+00321727 _pthread_start+000335
[ 63] 0x00007fff90d13b75                  /usr/lib/system/libsystem_c.dylib+00334709 thread_start+000013
At first I thought it was a problem of compiling against the CGAL that came with macports rather than a version inside on matlab. But actually it seems matlab no longer contains the libCGAL libraries. I did however see the boost libraries there. When I checked the libraries of my mex file using:
otool -L myfunction.mexmaci64
I saw:
myfunction.mexmaci64:
	/opt/local/lib/libCGAL.10.dylib (compatibility version 10.0.0, current version 10.0.0)
	/opt/local/lib/libCGAL_Core.10.dylib (compatibility version 10.0.0, current version 10.0.0)
	/opt/local/lib/libgmp.10.dylib (compatibility version 11.0.0, current version 11.5.0)
	/opt/local/lib/libmpfr.4.dylib (compatibility version 6.0.0, current version 6.1.0)
	/opt/local/lib/libboost_thread-mt.dylib (compatibility version 0.0.0, current version 0.0.0)
	/opt/local/lib/libboost_system-mt.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libmx.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libmat.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libmex.dylib (compatibility version 0.0.0, current version 0.0.0)
	/opt/local/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.17.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
	/opt/local/lib/gcc47/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
libboost_*-mt are the boost libraries that come from macports. Inside of matlab these are just libboost_* (without the -mt). After switching my make file to link against these:
-L$MATLAB/bin/maci64/ -lboost_thread -lboost_system
I used otool again and confirmed that I linked against matlab's boost:
myfunction.mexmaci64:
	/opt/local/lib/libCGAL.10.dylib (compatibility version 10.0.0, current version 10.0.0)
	/opt/local/lib/libCGAL_Core.10.dylib (compatibility version 10.0.0, current version 10.0.0)
	@rpath/libgmp.3.dylib (compatibility version 8.0.0, current version 8.1.0)
	@rpath/libmpfr.1.dylib (compatibility version 3.0.0, current version 3.2.0)
	@rpath/libboost_thread.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libboost_system.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libmx.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libmat.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libmex.dylib (compatibility version 0.0.0, current version 0.0.0)
	/opt/local/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.17.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
	/opt/local/lib/gcc47/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
I tried running my mex file and indeed this worked. Update: This also happens when calling mex directly. Somehow matlab still finds the wrong boost libs.