Embedding Python - Deleting a class instance

  • Thread starter Bue Krogh Vedel-Larsen
  • Start date
B

Bue Krogh Vedel-Larsen

How do I delete a class instance created using PyInstance_New? I've tried
calling Py_CLEAR on the instance, but __del__ isn't called. I've also tried
calling PyObject_Del, but this gives an access violation in
_PyObject_DebugDumpAddress so I guess that ain't a good idea :)

I've noticed that the PyObject returned by PyInstance_New has refcount = 2,
does this have any significance?
 
D

Denis S. Otkidach

How do I delete a class instance created using PyInstance_New? I've tried
calling Py_CLEAR on the instance, but __del__ isn't called. I've also tried
calling PyObject_Del, but this gives an access violation in
_PyObject_DebugDumpAddress so I guess that ain't a good idea :)

It is deleted automatically when reference count become zero or when
garbage collector runs if the object contains references to itself.
Just call Py_DECREF on it if you are not going to use it anymore.
I've noticed that the PyObject returned by PyInstance_New has refcount = 2,
does this have any significance?

The code below prints 1:

PyObject *obj = PyInstance_New(PyExc_Exception, 0, 0);
std::cerr << obj->ob_refcnt << "\n";

Does it create circular references in constructor?
 
J

Jeff Epler

I wrote the following module to test the behavior of PyInstance_New. I
called it something like this:
import vedel

class k:
def __del__(self): print "deleted"

vedel.g(k)

I get output like:
after creation, x->refcnt = 1
doing decref
deleted
after decref

Unless there's a cycle and GC gets involved, all there is to deleting
*anything* in Python is correctly managing the refcount. On the other
hand, you can never free an object while it is still reachable. Some
local name "x" may never spontaneously lose the thing it refers to.

/*------------------------------------------------------------------------*/
#include <Python.h>

static PyObject *g(PyObject *s, PyObject *o) {
PyObject *x = PyInstance_New( o, NULL, NULL);
if(x) {
printf("after creation, x->refcnt = %d\n", x->ob_refcnt);
printf("doing decref\n");
Py_DECREF(x);
printf("after decref\n");
} else {
printf("x == NULL\n");
}
Py_INCREF(Py_None);
return Py_None;
}

static PyMethodDef methods[] = {
{"g", (PyCFunction)g, METH_O, NULL},
{NULL},
};

void initvedel(void) {
Py_InitModule("vedel", methods);
}
/*------------------------------------------------------------------------*/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCuAkbJd01MZaTXX0RAkLtAKCXLJJftOWuA8bpI789UtgjQQqHZQCfYXdC
XR8wecuPBv9UpER7Rz0h/M8=
=XFo0
-----END PGP SIGNATURE-----
 
B

Bue Krogh Vedel-Larsen

Unless there's a cycle and GC gets involved, all there is to deleting
*anything* in Python is correctly managing the refcount. On the other
hand, you can never free an object while it is still reachable. Some
local name "x" may never spontaneously lose the thing it refers to.

Thanks for the replies. I've solved it, it was a kind of cycle: When
creating the instance it registered itself with my code, and was supposed
to unregister itself when deleted, but since I kept a copy of the object in
the program it never got unregistered, and thus never deleted...
 
A

Andreas Kostyrka

How do I delete a class instance created using PyInstance_New? I've tried
calling Py_CLEAR on the instance, but __del__ isn't called. I've also tried
calling PyObject_Del, but this gives an access violation in
_PyObject_DebugDumpAddress so I guess that ain't a good idea :)

I've noticed that the PyObject returned by PyInstance_New has refcount = 2,
does this have any significance?
Well, the only way to do that is to call PyDECREF. And this is only legal
when you really remove the reference, or else you get dangling pointers
which will lead to a corrupted heap and/or segfault somewhere in the future of
your program.

Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top