[Numpy-discussion] bus error in embedded numpy

Robert Kern robert.kern@gmail....
Fri Nov 13 12:13:39 CST 2009


On Fri, Nov 13, 2009 at 12:05, Travis Oliphant <oliphant@enthought.com> wrote:
>
> On Nov 13, 2009, at 11:23 AM, Robin 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).
>
> I wonder if this is related to the fact that you can't "unload" a
> dynamically linked module (like NumPy has).   So, when you call
> Py_Finalize you are not really "finalizing" your usage of Python
> extension modules.
>
> I'm not sure though.

Right. We do some global things when numpy is imported. Since there is
no unload step for extension modules, we can't undo them. The second
time the interpreter starts up, it doesn't know that numpy has already
been loaded and that numpy shouldn't try to do those global things
again.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the NumPy-Discussion mailing list