In my program I manage references to python objects in C++. I.e. all my classes are derived from class Referenced, which contains pointer to corresponding python object.
class Referenced
{
public:
unsigned use_count() const
{
return selfptr->ob_refcnt;
}
void add_ref() const
{
Py_INCREF(selfptr);
}
void remove_ref() const
{
Py_DECREF(selfptr);
}
PyObject* selfptr;
};
I use intrusive_ptr to hold objects derived from Referenced. This allows me to easily keep references to required python objects in C++ and access them whether necessary. But my program crashes (only in windows howewer) when python object is going to be deleted from C++, i.e. when I call Py_DECREF(selfptr), whether selfptr->ob_refcnt == 1. Is this approach OK?
Upd: I finally figured out problem in my program. It wasn’t related directly to object removal. To check the initial question I’ve implemented simple extension module remembering reference to python object and releasing it on demand. Here is it:
#include <Python.h>
static PyObject* myObj;
static PyObject* acquirePythonObject(PyObject* self, PyObject* obj)
{
printf("trying to acquire python object %p, refcount = %d\n", obj, obj->ob_refcnt);
myObj = obj;
Py_INCREF(myObj);
printf("reference acquired\n");
return Py_True;
}
static PyObject* freePythonObject(PyObject*, PyObject*)
{
printf("trying to free python object %p, refcount = %d\n", myObj, myObj->ob_refcnt);
Py_DECREF(myObj);
printf("reference removed\n");
return Py_True;
}
static PyMethodDef moduleMethods[] =
{
{"acquirePythonObject", acquirePythonObject, METH_O, "hold reference to python object."},
{"freePythonObject", freePythonObject, METH_NOARGS, "free reference to python object."},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC initmodule(void)
{
Py_InitModule("module", moduleMethods);
}
And python script:
import module
class Foo:
def __init__(self):
print "Foo is created"
def __deinit__(self):
print "Foo is destroyed"
def acquireFoo():
foo = Foo()
module.acquirePythonObject(foo)
def freeFoo():
module.freePythonObject()
if __name__ == "__main__":
acquireFoo()
freeFoo()
Sample runs seamlessly in windows and linux. Below is the output.
Foo is created
trying to acquire python object 0x7fa19fbefd40, refcount = 2
reference acquired
trying to free python object 0x7fa19fbefd40, refcount = 1
Foo is destoryed
reference removed
Basically, but …
add_ref/remove_refare called the correct number of times (using RAII would automate this – maybe that’s what your intrusive_ptr does?)remove_reftoo many times, I’m not sure what Python guarantees. If you setselfptr = NULLwhen you know the refcount is going from 1 -> 0, you could catch thisPy_XDECREFPy_CLEARinsteadAnd finally … do you have any crash dump or diagnostic info?