I forgot to add it doesn't happen if Py_Finalize isn't called - if I take that out and just let the OS/matlab unload the mex function then I can run it many times without the error, but it does leak a bit of memory each time.
Cheers Robin On Fri, Nov 13, 2009 at 5:23 PM, Robin <robi...@gmail.com> wrote: > Hi, > > I'm trying to embed Python in a MATLAB mex file. I've been coming > under some pressure to make my Python code available to my MATLAB > colleagues so I am trying to come up with a relatively general way of > calling numerical python code from Matlab. > > I am making some progress - but get a reliable crash from numpy. This > only occurs the second time I am loading it. So I Py_Initialize, > import numpy, Py_Finalize (all works fine), but then if I clear the > mex file (clear funcname in matlab - which calls Py_Finalize through a > mexAtExit handler) and try to run the function again (which will > reinitialize interpreter and import numpy again) I get the followin > stack trace from multisarray.so. Wondered if anyone could through any > light. I have already run into this bug > http://bugs.python.org/issue6869 which prevents me using ctypes... I > wondered if this was related. > > For now its not such a big problem - I will just avoid unloading the > mex function (with clear function). But I thought it might be > indicative of a memory leak or some other problem since I think in > theory it should work (It does if numpy isn't imported). > > Cheers > > Robin > > ------------------------------------------------------------------------ > Bus error detected at Fri Nov 13 17:11:57 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 = 00000001 ebx = 307b12ab > ecx = 00000000 edx = 305ef148 > esi = 305ef140 edi = 32a79d60 > ebp = b097c6c8 esp = b097c670 > eip = 307b14be flg = 00010202 > > Stack Trace: > [0] multiarray.so:PyArray_FromScalar~(0x305ef140, 0, 2, 0x32e23287) > + 542 bytes > [1] multiarray.so:gentype_nonzero_number~(0x305ef140, 0x2eaa6db0, 0, > 0x32e73cfe) + 36 bytes > [2] Python:PyObject_IsTrue~(0x305ef140, 0x2ea95de0, 2, 0) + 63 bytes > [3] Python:PyEval_EvalFrameEx~(0x33c95160, 0, 0x332941e0, 0) + 12598 bytes > [4] Python:PyEval_EvalFrameEx~(0x32a70f80, 0, 0x332941e0, 0) + 24217 bytes > [5] Python:PyEval_EvalCodeEx~(0x33f05068, 0x332941e0, 0, 0x32a96794) > + 1819 bytes > [6] Python:PyEval_EvalFrameEx~(0x32a96640, 0, 0x332941e0, 0) + 16561 bytes > [7] Python:PyEval_EvalCodeEx~(0x33f051d0, 0x332941e0, 0, 0x32a6fcc0) > + 1819 bytes > [8] Python:PyEval_EvalFrameEx~(0x32a6fb60, 0, 0x33bc99c0, 0) + 16561 bytes > [9] Python:PyEval_EvalCodeEx~(0x334abda0, 0x33bc99c0, 0, 0x32ad3b24) > + 1819 bytes > [10] Python:PyEval_EvalFrameEx~(0x32ad39d0, 0, 0x33bc94b0, 0) + 16561 bytes > [11] Python:PyEval_EvalCodeEx~(0x332919b0, 0x33bc94b0, 0, > 0x33ce5294) + 1819 bytes > [12] Python:PyEval_EvalFrameEx~(0x33ce5150, 0, 0x33bc94b0, 0) + 16561 bytes > [13] Python:PyEval_EvalCodeEx~(0x332918d8, 0x33bc94b0, 0, > 0x34d5156c) + 1819 bytes > [14] Python:PyEval_EvalFrameEx~(0x34d51430, 0, 0x33bc9300, > 0x33bc9300) + 16561 bytes > [15] Python:PyEval_EvalCodeEx~(0x33291968, 0x33bc9300, 0x33bc9300, > 0) + 1819 bytes > [16] Python:PyEval_EvalCode~(0x33291968, 0x33bc9300, 0x33bc9300, > 0x9325378f) + 87 bytes > [17] Python:PyImport_ExecCodeModuleEx~(0xb097e4cb > "numpy.core._internal", 0x33291968, 0xb097dbbf > "/Library/Frameworks/Python.frame..", 0x332a2000) + 193 bytes > [18] Python:load_source_module~(1, 0, 0xb097e418 "X™C†", 0xb097e41c) > + 726 bytes > [19] Python:import_submodule~(0xb097e4d6 "_internal", 0x33d1ce1c > "_internal", 9, 0x32e2cfc9) + 293 bytes > [20] Python:load_next~(0xb097e4cb "numpy.core._internal", > 0xb097e8cc, 0xb097e578, 0x32e8b5e6) + 195 bytes > [21] Python:import_module_level~(0x32ee4aa0 "TD", 0xffffffff, > 0xee9e70b3, 0x32e6df4d "«E") + 142 bytes > [22] Python:PyImport_ImportModuleLevel~(0x33d1ce1c "_internal", > 0x33b25150, 0x33b25150, 0x32ee4aa0 "TD") + 45 bytes > [23] Python:builtin___import__~(0, 0x348854e0, 0, 0x32e73cfe) + 156 bytes > [24] Python:PyObject_Call~(0x33da9a08, 0x348854e0, 0, 0x32e233cd) + 45 bytes > [25] Python:PyEval_CallObjectWithKeywords~(0x33da9a08, 0x348854e0, > 0, 0x33b25150) + 112 bytes > [26] Python:PyEval_EvalFrameEx~(0x33cd2da0, 0, 0x33b25150, > 0x33b25150) + 8138 bytes > [27] Python:PyEval_EvalCodeEx~(0x33332068, 0x33b25150, 0x33b25150, > 0) + 1819 bytes > [28] Python:PyEval_EvalCode~(0x33332068, 0x33b25150, 0x33b25150, > 0x9325378f) + 87 bytes > [29] Python:PyImport_ExecCodeModuleEx~(0xb097fadb "numpy.core", > 0x33332068, 0xb097ed7f "/Library/Frameworks/Python.frame..", > 0x4adc73cc) + 193 bytes > [30] Python:load_source_module~(1, 0, 0xb097f5cc, 0) + 726 bytes > [31] Python:load_package~(5, 0, 0xb097fa28, 0xb097fa2c) + 427 bytes > [32] Python:import_submodule~(0xb097fae1 "core", 0x3488536a > "core.numeric", 4, 0x32e2cfc9) + 293 bytes > [33] Python:load_next~(0xb097fadb "numpy.core", 0xb097fedc, 9, > 0x32e8b5e6) + 195 bytes > [34] Python:import_module_level~(0x32ee4aa0 "TD", 0xffffffff, > 0x746e6920, 0x32e6df4d "«E") + 213 bytes > [35] Python:PyImport_ImportModuleLevel~(0x34885364 > "numpy.core.numeric", 0x33b25a50, 0x33b25a50, 0x32ee4aa0 "TD") + 45 > bytes > [36] Python:builtin___import__~(0, 0x34885780, 0, 0x32e73cfe) + 156 bytes > [37] Python:PyObject_Call~(0x33da9a08, 0x34885780, 0, 0x32e233cd) + 45 bytes > [38] Python:PyEval_CallObjectWithKeywords~(0x33da9a08, 0x34885780, > 0, 0x33b25a50) + 112 bytes > [39] Python:PyEval_EvalFrameEx~(0x33cd07a0, 0, 0x33b25a50, > 0x33b25a50) + 8138 bytes > [40] Python:PyEval_EvalCodeEx~(0x33332140, 0x33b25a50, 0x33b25a50, > 0) + 1819 bytes > [41] Python:PyEval_EvalCode~(0x33332140, 0x33b25a50, 0x33b25a50, > 0x9325378f) + 87 bytes > [42] Python:PyImport_ExecCodeModuleEx~(0xb0980c9b > "numpy.lib.type_check", 0x33332140, 0xb098038f > "/Library/Frameworks/Python.frame..", 6771) + 193 bytes > [43] Python:load_source_module~(1, 0, 0xb0980be8 "®©C†", 0xb0980bec) > + 726 bytes > [44] Python:import_submodule~(0xb0980ca5 "type_check", 0x33d182b4 > "type_check", 10, 0x32e2cfc9) + 293 bytes > [45] Python:load_next~(0xb0980c9b "numpy.lib.type_check", > 0xb098109c, 0xb0980d48, 0x32e8b5e6) + 195 bytes > [46] Python:import_module_level~(0x3082c1b0, 0xffffffff, 0xee9e70b3, > 0x32e6df4d "«E") + 142 bytes > [47] Python:PyImport_ImportModuleLevel~(0x33d182b4 "type_check", > 0x33b25390, 0x33b25390, 0x3082c1b0) + 45 bytes > [48] Python:builtin___import__~(0, 0x348d0d20, 0, 0x32e73cfe) + 156 bytes > [49] Python:PyObject_Call~(0x33da9a08, 0x348d0d20, 0, 0x32e233cd) + 45 bytes > [50] Python:PyEval_CallObjectWithKeywords~(0x33da9a08, 0x348d0d20, > 0, 0x33b25390) + 112 bytes > [51] Python:PyEval_EvalFrameEx~(0x33ce5580, 0, 0x33b25390, > 0x33b25390) + 8138 bytes > [52] Python:PyEval_EvalCodeEx~(0x337b14e8, 0x33b25390, 0x33b25390, > 0) + 1819 bytes > [53] Python:PyEval_EvalCode~(0x337b14e8, 0x33b25390, 0x33b25390, > 0x9325378f) + 87 bytes > [54] Python:PyImport_ExecCodeModuleEx~(0xb09822ab "numpy.lib", > 0x337b14e8, 0xb098154f "/Library/Frameworks/Python.frame..", > 0x4adc73cd) + 193 bytes > [55] Python:load_source_module~(1, 0, 0xb0981d9c, 0) + 726 bytes > [56] Python:load_package~(5, 0, 0xb09821f8, 0xb09821fc) + 427 bytes > [57] Python:import_submodule~(0xb09822b1 "lib", 0x2ea9f074 "lib", 3, > 0x32e2cfc9) + 293 bytes > [58] Python:load_next~(0xb09822ab "numpy.lib", 0xb09826ac, 5, > 0x32e8b5e6) + 195 bytes > [59] Python:import_module_level~(0x33768ab0, 0xffffffff, 0xb0982738, > 0x32e6df4d "«E") + 142 bytes > [60] Python:PyImport_ImportModuleLevel~(0x2ea9f074 "lib", > 0x33b25420, 0x33b25420, 0x33768ab0) + 45 bytes > [61] Python:builtin___import__~(0, 0x348d0f30, 0, 0x32e73cfe) + 156 bytes > [62] Python:PyObject_Call~(0x33da9a08, 0x348d0f30, 0, 0x32e233cd) + 45 bytes > [63] Python:PyEval_CallObjectWithKeywords~(0x33da9a08, 0x348d0f30, > 0, 0x33b25420) + 112 bytes > [64] Python:PyEval_EvalFrameEx~(0x32a90300, 0, 0x33b25420, > 0x33b25420) + 8138 bytes > [65] Python:PyEval_EvalCodeEx~(0x337b13c8, 0x33b25420, 0x33b25420, > 0) + 1819 bytes > [66] Python:PyEval_EvalCode~(0x337b13c8, 0x33b25420, 0x33b25420, > 0x9325378f) + 87 bytes > [67] Python:PyImport_ExecCodeModuleEx~(0xb098346b > "numpy.add_newdocs", 0x337b13c8, 0xb0982b5f > "/Library/Frameworks/Python.frame..", 0x0358d500) + 193 bytes > [68] Python:load_source_module~(1, 0, 0xb09833b8 "¯®C†", 0xb09833bc) > + 726 bytes > [69] Python:import_submodule~(0xb0983471 "add_newdocs", 0x33d0a7b4 > "add_newdocs", 11, 0x32e2cfc9) + 293 bytes > [70] Python:load_next~(0xb098346b "numpy.add_newdocs", 0xb098386c, > 0xb0983518, 0x32e8b5e6) + 195 bytes > [71] Python:import_module_level~(0x32ee4aa0 "TD", 0xffffffff, > 0xe17a9024, 0x32e6df4d "«E") + 142 bytes > [72] Python:PyImport_ImportModuleLevel~(0x33d0a7b4 "add_newdocs", > 0x33b25b70, 0x33b25b70, 0x32ee4aa0 "TD") + 45 bytes > [73] Python:builtin___import__~(0, 0x348c7ed0, 0, 0x32e73cfe) + 156 bytes > [74] Python:PyObject_Call~(0x33da9a08, 0x348c7ed0, 0, 0x32e233cd) + 45 bytes > [75] Python:PyEval_CallObjectWithKeywords~(0x33da9a08, 0x348c7ed0, > 0, 0x33b25b70) + 112 bytes > [76] Python:PyEval_EvalFrameEx~(0x34c489a0, 0, 0x33b25b70, > 0x33b25b70) + 8138 bytes > [77] Python:PyEval_EvalCodeEx~(0x337562f0, 0x33b25b70, 0x33b25b70, > 0) + 1819 bytes > [78] Python:PyEval_EvalCode~(0x337562f0, 0x33b25b70, 0x33b25b70, > 0x9325378f) + 87 bytes > [79] Python:PyImport_ExecCodeModuleEx~(0xb0984a7b "numpy", > 0x337562f0, 0xb0983d1f "/Library/Frameworks/Python.frame..", > 0x4adc73cd) + 193 bytes > [80] Python:load_source_module~(1, 0, 0xb098456c, 0) + 726 bytes > [81] Python:load_package~(5, 0, 0xb09849c8, 0xb09849cc) + 427 bytes > [82] Python:import_submodule~(0xb0984a7b "numpy", 0x304f6134 > "numpy", 5, 0x32e21553) + 293 bytes > [83] Python:load_next~(0xb0984a7b "numpy", 0xb0984e7c, 0xb0984b28, > 0x32e8b5e6) + 195 bytes > [84] Python:import_module_level~(0x32ee4aa0 "TD", 0xffffffff, > 0x30173290, 0x32e6df4d "«E") + 142 bytes > [85] Python:PyImport_ImportModuleLevel~(0x304f6134 "numpy", > 0x33f19420, 0x33f19420, 0x32ee4aa0 "TD") + 45 bytes > [86] Python:builtin___import__~(0, 0x33341bd0, 0, 0x32e73cfe) + 156 bytes > [87] Python:PyObject_Call~(0x33da9a08, 0x33341bd0, 0, 0x32e233cd) + 45 bytes > [88] Python:PyEval_CallObjectWithKeywords~(0x33da9a08, 0x33341bd0, > 0, 0x33f19420) + 112 bytes > [89] Python:PyEval_EvalFrameEx~(0x336e5be0, 0, 0x33f19420, > 0x33f19420) + 8138 bytes > [90] Python:PyEval_EvalCodeEx~(0x2eabb9b0, 0x33f19420, 0x33f19420, > 0) + 1819 bytes > [91] Python:PyEval_EvalCode~(0x2eabb9b0, 0x33f19420, 0x33f19420, > 0x33684d60) + 87 bytes > [92] Python:PyRun_StringFlags~(0x2dd56f19 "import numpy; > numpy.test()", 257, 0x33f19420, 0x33f19420) + 243 bytes > [93] Python:PyRun_SimpleStringFlags~(0x2dd56f19 "import numpy; > numpy.test()", 0, 0, 0xb0985b88) + 72 bytes > [94] libmex.dylib:mexRunMexFile(0, 0xb0985b28, 0, 0xb0985b88) + 107 bytes > [95] libmex.dylib:Mfh_mex::runMexFileWithSignalProtection(int, > mxArray_tag**, int, mxArray_tag**)(0x301beb70, 0, 0xb0985b28, 0) + 111 > bytes > [96] libmex.dylib:Mfh_mex::dispatch_file(int, mxArray_tag**, int, > mxArray_tag**)(0x301beb70, 0, 0xb0985b28, 0) + 250 bytes > [97] libmwm_dispatcher.dylib:Mfh_file::dispatch_fh(int, > mxArray_tag**, int, mxArray_tag**)(0x301beb70, 0, 0xb0985b28, 0) + 256 > bytes > [98] libmwm_interpreter.dylib:ResolverFunctionDesc::CallFunction(int, > mxArray_tag**, int, mxArray_tag**)(0xb0986118 "‡Gs", 0, 0xb0985b28, 0) > + 793 bytes > [99] libmwm_interpreter.dylib:Resolver::CallMFunction(int, int, > _m_operand*, m_operand_storage*, int, _m_operand*, m_operand_storage*, > int*)(0xb0985cf4, 0, 1, 0x301e3f50) + 1446 bytes > [100] libmwm_interpreter.dylib:inResolveMFunctionCall(_m_function_desc*, > int, int, _m_operand*, m_operand_storage*, int, _m_operand*, > m_operand_storage*, int*, inMarshalType*, int, mpsTypeSequenceNlhs > const*, mxArray_tag* (*)(int))(0x301beef0, 0, 1, 0x301e3f50) + 474 > bytes > [101] > libmwm_interpreter.dylib:accelImpl::MFunctionCall(_accelOp**)(0xb0986264, > 0xb0986278 "XT$.", 0x336380b0, 0x34c89470 "∞<n3") + 269 bytes > [102] libmwm_interpreter.dylib:accelImpl::Exec()(0xb0986264, > 0x0358a000, 0xb09862a8, 0x336bb9b4) + 199 bytes > [103] libmwm_interpreter.dylib:accelCode::Call(inMarshalType*, int*) > const(0x336cd430, 0xb09863d8, 0xb09863d4, 0x009d6e60) + 100 bytes > [104] libmwm_interpreter.dylib:inJit::ExecuteHotSegment(_inJitAccelInfo*, > opcodes*, int*, int*)(0xb0986458, 0xb0986468, 0xb0986464 "ˇˇˇˇ", > 0xb0986460) + 1338 bytes > [105] libmwm_interpreter.dylib:inExecuteMFunctionOrScript(Mfh_mp*, > bool)(0x336e3cb0, 1, 0xb098699c, 0) + 704 bytes > [106] libmwm_interpreter.dylib:inRunMfile(int, mxArray_tag**, int, > mxArray_tag**, Mfh_mp*, inWorkSpace_tag*)(0, 0xb098699c, 0, 0) + 696 > bytes > [107] libmwm_interpreter.dylib:Mfh_mp::dispatch_file(int, > mxArray_tag**, int, mxArray_tag**)(0x336e3cb0, 0, 0xb098699c, 0) + 56 > bytes > [108] libmwm_dispatcher.dylib:Mfh_file::dispatch_fh(int, > mxArray_tag**, int, mxArray_tag**)(0x336e3cb0, 0, 0xb098699c, 0) + 256 > bytes > [109] libmwm_interpreter.dylib:inEvalPcodeHeaderToWord(_memory_context*, > int, mxArray_tag**, _pcodeheader*, Mfh_mp*, unsigned int)(0x000a2ac8 > "§*\n", 0, 0xb098699c, 0xb098683c) + 252 bytes > [110] libmwm_interpreter.dylib:inEvalStringWithIsVarFcn(_memory_context*, > char const*, EvalType, int, mxArray_tag**, inDebugCheck, > _pcodeheader*, int*, bool (*)(void*, char const*), void*, bool, > bool)(0, 0xb098699c, 0, 0) + 1835 bytes > [111] libmwm_interpreter.dylib:inEvalCmdWithLocalReturn(char const*, > int*, bool, bool, bool (*)(void*, char const*))(1, 0x004aeb20, 0, 0) + > 148 bytes > [112] libmwm_interpreter.dylib:inEvalCmdWithLocalReturn(0x2e272200 > "pytest()\n", 0, 0, 1) + 66 bytes > [113] libmwbridge.dylib:evalCommandWithLongjmpSafety(char > const*)(0x2e272200 "pytest()\n", 2, 0x3012ff98, 4073223) + 108 bytes > [114] libmwbridge.dylib:mnParser(0xb0986b34, 0x0502cc00, 1, 0) + 666 bytes > [115] > libmwmcr.dylib:mcrInstance::mnParser_on_interpreter_thread()(0x0502cc00, > 8, 0x0384ce00, 4) + 43 bytes > [116] libmwmcr.dylib:boost::function0<void>::operator()() > const(0x2b44dc54 "ˆA", 0, 0xb0986c68, 171953) + 41 bytes > [117] > libmwmcr.dylib:mcr::runtime::InterpreterThread::Impl::NoResultInvocationRequest::run()(0x2b44dc40, > 0, 0xb0986be8, 172001) + 21 bytes > [118] > libmwmcr.dylib:mcr::runtime::InterpreterThread::Impl::invocation_request_handler(long)(0x2b44dc40, > 0, 0x027a1a00, 0x3005f880) + 24 bytes > [119] libmwuix.dylib:uix_DispatchOrProcess(_XEvent*, _XtAppStruct*, > int, bool)(0, 15, 0x000f4240 "put_target_string_with_length", > 0x4afd9271) + 476 bytes > [120] libmwuix.dylib:ws_ProcessPendingEventsHelper(int, int, > bool)(1, 0xffffffff, 0, 852585) + 469 bytes > [121] > libmwmcr.dylib:mcr::runtime::InterpreterThread::Impl::process_events(boost::shared_ptr<mcr::runtime::InterpreterThread::Impl> > const&)(0x04c3b8e0, 0xb0986e68, 0, 0x04c3b890 > "/Applications/MATLAB_R2009a.app/..") + 376 bytes > [122] > libmwmcr.dylib:mcr::runtime::InterpreterThread::Impl::run(boost::shared_ptr<mcr::runtime::InterpreterThread::Impl> > const&, mcr::runtime::InterpreterThread::Impl::init_context*)(0x04c3b8e0, > 0xb0986e68, 0xb0a06cac, 4078061) + 410 bytes > [123] libmwmcr.dylib:run_init_and_handle_events(void*)(0xb0a06cac, > 0, 0, 0) + 52 bytes > [124] MATLAB:create_mcrInstance_and_run_mnParser(0xb0986f00 "@†A", > 8816, 0, 0) + 553 bytes > [125] MATLAB:start(2, 0xbffff4d0 "§ıˇøÓıˇø", 0xb0987000 "DRHT", > 0xffffffff) + -2223 bytes > [126] libmwmcr.dylib:runMcrMain(void*)(0xbffff440 "‡%", 0x04000000, > 1, 0) + 39 bytes > [127] libSystem.B.dylib:_pthread_start~(0xb0987000 "DRHT", 14083, > 0x004075f0, 0xbffff440 "‡%") + 345 bytes > [128] libSystem.B.dylib:thread_start~(0, 0, 0, 0x54485244) + 34 bytes > _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion