Re: Embedding Python - Deleting a class instance

2005-06-22 Thread Andreas Kostyrka
On Tue, Jun 21, 2005 at 12:06:47PM +, 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Embedding Python - Deleting a class instance

2005-06-21 Thread 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?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Embedding Python - Deleting a class instance

2005-06-21 Thread Denis S. Otkidach
On Tue, 21 Jun 2005 12:06:47 GMT
Bue Krogh Vedel-Larsen [EMAIL PROTECTED] 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]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Embedding Python - Deleting a class instance

2005-06-21 Thread Bue Krogh Vedel-Larsen
Jeff Epler [EMAIL PROTECTED] wrote in
news:[EMAIL PROTECTED]: 

 
 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...
-- 
http://mail.python.org/mailman/listinfo/python-list