Hi, I am trying to embed Python in a MATLAB mex function. This is loaded into the MATLAB interpreter - I would like the Python interpreter to be initialized once and stay there for future calls. I added a call to Py_Finalize as a mexAtExit handler which is called when the library is unloaded in MATLAB (with clear funcname) or when MATLAB shuts down. So far, things were working well, but I get a random but easily repeatable bus error in Py_Finalize if I have imported ctypes.
Here is my code: #include <mex.h> #include <Python/Python.h> static int PYRUNNING = 0; static void Cleanup(void) { mexPrintf("Finalising Python...\n"); Py_Finalize(); } void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[]) { mexPrintf("hello from mex\n"); if (!PYRUNNING) { Py_Initialize(); PYRUNNING = 1; mexAtExit(Cleanup); PyRun_SimpleString("import ctypes"); } PyRun_SimpleString("print 'hello from python'"); } If I load, run the function, and unload many times eventually I get the following stack trace (whcih i don't get if I'm not import ctypes). Does anyone have any idea what might be up? Could it be a bug in Py_Finalize or ctypes? Is there a problem with my approach (ie is it bad to leave the interpreter initialized between mex function calls - it seems to work provided ctypes is not imported - but I need ctypes to access other mex functions from python). ------------------------------------------------------------------------ Bus error detected at Fri Nov 13 14:06:12 2009 ------------------------------------------------------------------------ Configuration: MATLAB Version: 7.8.0.347 (R2009a) MATLAB License: 161051 Operating System: Darwin 10.0.0 Darwin Kernel Version 10.0.0: Fri Jul 31 22:47:34 PDT 2009; root:xnu-1456.1.25~1/RELEASE_I386 i386 Window System: The X.Org Foundation (10402000), display /tmp/ launch-2p1ZWg/:0 Current Visual: 0x24 (class 4, depth 24) Processor ID: x86 Family 6 Model 15 Stepping 10, GenuineIntel Virtual Machine: Java 1.6.0_15-b03-219 with Apple Inc. Java HotSpot (TM) Client VM mixed mode Default Encoding: ISO-8859-1 Fault Count: 1 Register State: eax = 00000000 ebx = 3483d9be ecx = 33ff7620 edx = 00000000 esi = 32fb4dc0 edi = 0000001a ebp = b0b69938 esp = b0b69920 eip = 3485559f flg = 00010282 Stack Trace: [0] Python:type_dealloc~(0x32fb4dc0, 0x33f70818, 0x34854f0b, 0x34844410) + 26 bytes [1] Python:dict_dealloc~(0x33fef930, 0x348e4b40 "__builtin__", 0xb0b699bc, 0xb0b699b8) + 142 bytes [2] Python:dict_dealloc~(0x33f429c0, 0x348ee58c "exitfunc", 0xb0b699d8, 0x348b96b0) + 142 bytes [3] Python:_PyImport_Fini~(0x348ee58c "exitfunc", 0, 0, 0) + 82 bytes [4] Python:Py_Finalize~(673024, 1, 0, 0x00ab4700) + 207 bytes [5] libmex.dylib:SafeExitFunctionCall(void (*)())(0x30365df0, 0xb0b69ab8, 0xb0b69a80, 0x0073d01b) + 66 bytes [6] libmex.dylib:mexClearMexFileDefault(impl_info_tag*)(0x33e0dce0, 1, 0, 0x015fa3d8 "7Mfh_mex") + 80 bytes [7] libmex.dylib:safeMexClearMexFile(impl_info_tag*&)(0xb0b69bec "‡‹‡3", 1, 0x015fc080, 0) + 76 bytes [8] libmex.dylib:Mlm_mex::unload_file()(0x32b01290, 0, 0, 0) + 250 bytes [9] libmwm_dispatcher.dylib:Mlm_file::unload_mf()(0x32b01290, 0, 0, 0) + 18 bytes [10] libmwm_dispatcher.dylib:Mlm_MATLAB_fn::clear()(0x32b01290, 0, 0xb0b69c9c, 236) + 82 bytes [11] libmwm_dispatcher.dylib:void clear<Mlmmf_string_matcher> (Mlmmf_string_matcher, int)(0x05319c00, 0x33e24b8c "pytest", 0x30476300, 0x03f3b531 "pytest") + 296 bytes [12] libmwm_dispatcher.dylib:mdClearFunctionsByName(0x33e24b8c "pytest", 42, 0xb0b6adf8, 10) + 650 bytes [13] libmwm_interpreter.dylib:InterpBridge::FullClearFcn(int, mxArray_tag**, int, mxArray_tag**)(0x036225e0, 0, 0xb0b6af6c, 1) + 1234 bytes [14] libmwm_interpreter.dylib:inFullClearFcn(int, mxArray_tag**, int, mxArray_tag**)(0, 0xb0b6af6c, 1, 0xb0b6afcc) + 50 bytes [15] libmwm_dispatcher.dylib:Mfh_builtin::dispatch_mf(int, mxArray_tag**, int, mxArray_tag**)(0x2dd025a0, 0, 0xb0b6af6c, 1) + 95 bytes [16] libmwm_dispatcher.dylib:Mfh_MATLAB_fn::dispatch_fh(int, mxArray_tag**, int, mxArray_tag**)(0x2dd025a0, 0, 0xb0b6af6c, 1) + 192 bytes [17] libmwm_interpreter.dylib:inDispatchFromStack(int, char const*, int, int)(0, 0x304fa51c "clear", 0, 1) + 998 bytes [18] libmwm_interpreter.dylib:inDispatchCall(char const*, int, int, int, int*, int*)(1, 0xb0b6b298, 0x33e11a28, 0x9322e76b) + 152 bytes [19] libmwm_interpreter.dylib:inInterp(inDebugCheck, int, int, opcodes, inPcodeNest_tag volatile*, int*)(1, 0, 1, 0) + 5167 bytes [20] libmwm_interpreter.dylib:protected_inInterp(inDebugCheck, int, int, opcodes, inPcodeNest_tag*, int*)(1, 0, 1, 0) + 112 bytes [21] libmwm_interpreter.dylib:inInterPcodeSJ(inDebugCheck, int, int, opcodes, inPcodeNest_tag*, int*)(0, 0x33e007d0, 0xb0b6b460, 0xb0b6b460) + 266 bytes [22] libmwm_interpreter.dylib:inExecuteMFunctionOrScript(Mfh_mp*, bool)(0x33e263c0, 1, 0xb0b6b99c, 0) + 932 bytes [23] libmwm_interpreter.dylib:inRunMfile(int, mxArray_tag**, int, mxArray_tag**, Mfh_mp*, inWorkSpace_tag*)(0, 0xb0b6b99c, 0, 0) + 696 bytes [24] libmwm_interpreter.dylib:Mfh_mp::dispatch_file(int, mxArray_tag**, int, mxArray_tag**)(0x33e263c0, 0, 0xb0b6b99c, 0) + 56 bytes [25] libmwm_dispatcher.dylib:Mfh_file::dispatch_fh(int, mxArray_tag**, int, mxArray_tag**)(0x33e263c0, 0, 0xb0b6b99c, 0) + 256 bytes [26] libmwm_interpreter.dylib:inEvalPcodeHeaderToWord (_memory_context*, int, mxArray_tag**, _pcodeheader*, Mfh_mp*, unsigned int)(0x000a2ac8 "§*\n", 0, 0xb0b6b99c, 0xb0b6b83c) + 252 bytes [27] libmwm_interpreter.dylib:inEvalStringWithIsVarFcn (_memory_context*, char const*, EvalType, int, mxArray_tag**, inDebugCheck, _pcodeheader*, int*, bool (*)(void*, char const*), void*, bool, bool)(0, 0xb0b6b99c, 0, 0) + 1835 bytes [28] libmwm_interpreter.dylib:inEvalCmdWithLocalReturn(char const*, int*, bool, bool, bool (*)(void*, char const*))(1, 0x004aeb20, 0, 0) + 148 bytes [29] libmwm_interpreter.dylib:inEvalCmdWithLocalReturn(0x2e3e5e00 "clear pytest\n", 0, 0, 1) + 66 bytes [30] libmwbridge.dylib:evalCommandWithLongjmpSafety(char const*) (0x2e3e5e00 "clear pytest\n", 2, 0x2de28978, 4073223) + 108 bytes [31] libmwbridge.dylib:mnParser(0xb0b6bb34, 0x0501fc00, 1, 0) + 666 bytes [32] libmwmcr.dylib:mcrInstance::mnParser_on_interpreter_thread() (0x0501fc00, 8, 0x05016000, 4) + 43 bytes [33] libmwmcr.dylib:boost::function0<void>::operator()() const (0x2dd33904 "ˆA", 0, 0xb0b6bc68, 171953) + 41 bytes [34] libmwmcr.dylib:mcr::runtime::InterpreterThread::Impl::NoResultInvocationRequest::run ()(0x2dd338f0, 0, 0xb0b6bbe8, 172001) + 21 bytes [35] libmwmcr.dylib:mcr::runtime::InterpreterThread::Impl::invocation_request_handler (long)(0x2dd338f0, 0, 0xb0b6bc38, 3636709) + 24 bytes [36] libmwuix.dylib:uix_DispatchOrProcess(_XEvent*, _XtAppStruct*, int, bool)(0, 15, 0x000f4240 "put_target_string_with_length", 0x4afd6790) + 476 bytes [37] libmwuix.dylib:ws_ProcessPendingEventsHelper(int, int, bool)(1, 0xffffffff, 0, 852585) + 469 bytes [38] libmwmcr.dylib:mcr::runtime::InterpreterThread::Impl::process_events (boost::shared_ptr<mcr::runtime::InterpreterThread::Impl> const&) (0x036519b0, 0xb0b6be68, 0, 0x03651960 "/Applications/ MATLAB_R2009a.app/..") + 376 bytes [39] libmwmcr.dylib:mcr::runtime::InterpreterThread::Impl::run (boost::shared_ptr<mcr::runtime::InterpreterThread::Impl> const&, mcr::runtime::InterpreterThread::Impl::init_context*)(0x036519b0, 0xb0b6be68, 0xb0182cac, 4078061) + 410 bytes [40] libmwmcr.dylib:run_init_and_handle_events(void*)(0xb0182cac, 0, 0, 0) + 52 bytes [41] MATLAB:create_mcrInstance_and_run_mnParser(0xb0b6bf00 "@†A", 8816, 0, 0) + 553 bytes [42] MATLAB:start(2, 0xbffff4d0 "§ıˇøÓıˇø", 0xb0b6c000 "DRHT", 0xffffffff) + -2223 bytes [43] libmwmcr.dylib:runMcrMain(void*)(0xbffff440 "‡%", 0x04000000, 1, 0) + 39 bytes [44] libSystem.B.dylib:_pthread_start~(0xb0b6c000 "DRHT", 13827, 0x004075f0, 0xbffff440 "‡%") + 345 bytes [45] libSystem.B.dylib:thread_start~(0, 0, 0, 0x54485244) + 34 bytes -- http://mail.python.org/mailman/listinfo/python-list