6

I am new of python embedding. I am trying to embed python + numpy code inside a C++ callback function (inside a dll)

the problem i am facing is the following. if i have:

Py_Initialize();
// some python glue
// python invocation
Py_Finalize();

everything works fine.

but if i have:

Py_Initialize();
_import_array(); //to initialize numpy C-API
// some python glue + numpy array object creation
// python invocation via PyObject_CallObject()
Py_Finalize();

this crashes at the second time it reaches _import_array(); (meaning that it works for the first callback)

if i instead do the python and numpy initialization just once and the finalization in the destructor (thus not every time initializing/finalizing), everything crashes when leaving the callback..

The problem here i guess is numpy, but i dont know how to solve it

1
  • If you can fully control how the DLL is built, I would peronally leave the details to distutils and swig. Here is an example project (interfacing with numpy): github.com/martinxyz/python/tree/master/realistic Commented Sep 24, 2011 at 18:33

1 Answer 1

2

Try make sure your .dll is only initialized once, regardless of how many times the code is actually invoked.

Here is a link on "C++ Singleton in a DLL":

Singleton in a DLL?

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Paul, thanks for your answer! So, i am pretty sure my dll is initialized only once, and in fact, i omitted to say that if i also initialize python at the initialization of my dll, and finalize in the destructor, something like this works fine: PyRun_SimpleString( "from time import time,ctime\n" "from numpy import *\n" "print 'Today is',ctime(time())\n" ); but if i try to call python via PyObject_CallObject(), then it does not work anymore, and it crashes when the callback terminate
Hi - 1) Make sure you initialize Python only ONCE during program execution (and deinitialize no MORE than once, otherwise never). 2) Look at the martinxyz sample code posted above, 3) Look at this thread - a similar problem, caused by a reference count error: velocityreviews.com/forums/…
Thanks a lot Paul, this link helped a lot! in fact the problem was a DECREF of an object used by PyTuple_SetItem(). Now it seems to work, but i have to do some more extensive tests, of course! Thanks again!
aewsome I did this if (!Py_IsInitialized()){ ... initialize python interpreter ... }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.