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
1) Yes, I would recommend deleting the object. This will keep your code from getting bulky and/or slow. This is an especially good decision if you have a long run-time for your code, even though Python is pretty good about garbage collection.
The del keyword in python is primarily used to delete objects in Python. Since everything in python represents an object in one way or another, The del keyword can also be used to delete a list, slice a list, delete a dictionaries, remove key-value pairs from a dictionary, delete variables, etc.
To delete an object in Python, we use the 'del' keyword. A when we try to refer to a deleted object, it raises NameError.
__del__ is a finalizer. It is called when an object is garbage collected which happens at some point after all references to the object have been deleted.
Is this approach OK?
Basically, but ...
add_ref
/remove_ref
are called the correct number of times (using RAII would automate this - maybe that's what your intrusive_ptr does?)remove_ref
too many times, I'm not sure what Python guarantees. If you set selfptr = NULL
when you know the refcount is going from 1 -> 0, you could catch this
Py_XDECREF
Py_CLEAR
insteadAnd finally ... do you have any crash dump or diagnostic info?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With