Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to delete object allocated in python from c++? [closed]

Tags:

c++

python

api

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
like image 692
DikobrAz Avatar asked Jan 15 '13 16:01

DikobrAz


People also ask

Do we need to delete object in Python?

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.

Should I use del in Python?

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.

What does delete object do Python?

To delete an object in Python, we use the 'del' keyword. A when we try to refer to a deleted object, it raises NameError.

What is __ del __ in Python?

__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.


1 Answers

Is this approach OK?

Basically, but ...

  • I don't see any guarantee that add_ref/remove_ref are called the correct number of times (using RAII would automate this - maybe that's what your intrusive_ptr does?)
  • if you do try to 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
    • either by crashing hard, or by checking explicitly, or by using Py_XDECREF
    • even better, just use Py_CLEAR instead

And finally ... do you have any crash dump or diagnostic info?

like image 172
Useless Avatar answered Oct 17 '22 05:10

Useless