By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,996 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,996 IT Pros & Developers. It's quick & easy.

Embedding Python - Deleting a class instance

P: n/a
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?
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Tue, 21 Jun 2005 12:06:47 GMT
"Bue Krogh Vedel-Larsen" <he********@tiscali.dk> wrote:
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?

--
Denis S. Otkidach
http://www.python.ru/ [ru]
Jul 19 '05 #2

P: n/a
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)

iD8DBQFCuAkbJd01MZaTXX0RAkLtAKCXLJJftOWuA8bpI789Ut gjQQqHZQCfYXdC
XR8wecuPBv9UpER7Rz0h/M8=
=XFo0
-----END PGP SIGNATURE-----

Jul 19 '05 #3

P: n/a
Jeff Epler <je****@unpythonic.net> wrote in
news:ma**************************************@pyth on.org:

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...
Jul 19 '05 #4

P: n/a
On Tue, Jun 21, 2005 at 12:06:47PM +0000, Bue Krogh Vedel-Larsen wrote:
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

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.