Python interpreter crashes after trying to callback a python function with PyObject_CallObject from a dll
Hello guys, Im still implementing my callback to python from my dll and the python interpreter crashes I have no idea atm why I use the calldll module for calling my c++-dll from my module calldllsound.py: import calldll import struct class CallSoundDLL: def __init__(self, library): self.library = library def myGetResults(a): print a #just for debugging calldll.call_foreign_function (self.addrGet,'llllll','i',(self.ulHz.address(), self.dNoise.address(), self.bAudio.address(),self.bCrackFlag.address(), self.dSNR.address(), self.bBeep.address())) def mySetCallback(self): calldll.call_foreign_function (self.addrCall,'O','',(self.myGetResults,)) def start(self): # --------------------------------------------------------------------------------------------- # Get the handle of the dll and the address of the function # --------------------------------------------------------------------------------------------- #Get the handle of the Dll handle = calldll.load_library (self.library) #Get the address of the functions self.addrStart = calldll.get_proc_address (handle, 'StartProcessing') self.addrEnd = calldll.get_proc_address (handle, 'EndProcessing') self.addrGet = calldll.get_proc_address (handle, 'GetResults') self.addrCall = calldll.get_proc_address (handle, 'my_set_callback') # --------------------------------------------------------------------------------------------- # Initialization # --------------------------------------------------------------------------------------------- self.ulHz = calldll.membuf(4) #unsigned long* self.dNoise = calldll.membuf(8) #double* self.bAudio = calldll.membuf(4) #int* self.bCrackFlag = calldll.membuf(4) #int* self.dSNR = calldll.membuf(8) #double* self.bBeep = calldll.membuf(4) #int* # --------------------------------------------------------------------------------------------- # Function calling # --------------------------------------------------------------------------------------------- args = (1,1,1,1,1,1000) calldll.call_foreign_function (self.addrStart,'iiiiik','i',args) So Im starting my dll on such a way:
import calldllsound
obj = calldllsound.CallSoundDLL("soundproclib.dll")
obj.start()
It works without any problems. But after the following command crashes the python interpreter...:
obj.mySetCallback()
Here is my_set_callback function from my dll, which have to be called in the python function mySetCallback: static PyObject *my_callback = NULL; PyMODINIT_FUNC my_set_callback(PyObject *args) { PyObject *temp = args; Py_XINCREF(temp); /* Add a reference to new callback */ Py_XDECREF(my_callback); /* Dispose of previous callback */ my_callback = temp; /* Remember new callback */ Py_INCREF(Py_None); bPyCallBackFlag = true; }; and here is the part of my code from the same c++-file where Im trying to call my python-function myGetResults: if (bPyCallBackFlag) { /* Time to call the callback */ ppyobjArgList = Py_BuildValue("(i)",123); //just for debugging // Here crashes the python interpreter, I found it out with // error logging: ppyobjResult = PyObject_CallObject(my_callback, ppyobjArgList); Py_DECREF(ppyobjResult); } Have anybody any idea why it doesnt work? Thank you very much Oleg --------------------------------- Gesendet von Yahoo! Mail - Jetzt mit 1GB kostenlosem Speicher
On Wed, 10 Aug 2005 12:55:40 +0200 (CEST) Oleg Novychny <oleg_python@yahoo.de> wrote:
Have anybody any idea why it doesn_t work?
Thank you very much Oleg
Maybe you haven't acquired the GIL ? Simon. -- Simon Burton, B.Sc. Licensed PO Box 8066 ANU Canberra 2601 Australia Ph. 61 02 6249 6940 http://arrowtheory.com
static PyObject *my_callback = NULL; PyMODINIT_FUNC my_set_callback(PyObject *args) { PyObject *temp = args; Py_XINCREF(temp); /* Add a reference to new callback */ Py_XDECREF(my_callback); /* Dispose of previous callback */ my_callback = temp; /* Remember new callback */ Py_INCREF(Py_None);
bPyCallBackFlag = true; };
That looks suspect: * PyMODINIT_FUNC is probably not what you want - this is not a "module init" function. * The first arg is always "self", with "args" being a second arg. * You Py_INCREF(Py_None), but don't return it. I'm surprised that compiles. Let's say "args" was NULL (as it probably will be - recall it is supposed to be "self", which generally *will* be NULL. It should be obvious how that would crash your code. Also, note that even if you get the correct "args" pointer to that function, it will be a tuple. Hence all code you will find uses PyArg_ParseTuple to get the individual items out. Your callback will be the first item in the tuple.
ppyobjResult = PyObject_CallObject(my_callback, ppyobjArgList);
This will fail for that reason - my_callback will *not* be a callable object. I don't think either of the mailing lists you have sent this to are appropriate - the problems are not releated to win32, not are they related to C++. You really just need to look over the documentation on writing extension modules. Mark.
participants (3)
-
Mark Hammond -
Oleg Novychny -
Simon Burton