[C++-sig] [python-win32] Python interpreter crashes after trying to callbacka python function with PyObject_CallObject from a dll

Mark Hammond mhammond at skippinet.com.au
Thu Aug 11 00:45:59 CEST 2005

> 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"
* 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

> ppyobjResult = PyObject_CallObject(my_callback, ppyobjArgList);

This will fail for that reason - my_callback will *not* be a callable

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.


More information about the Cplusplus-sig mailing list