Re: dealloc function in python extend c module

2009-07-02 Thread Philip Semanchuk


On Jul 2, 2009, at 2:11 AM, Shen, Yu-Teh wrote:

I create my extend type something like http://www.python.org/doc/current/extending/newtypes.html 
.

And my type has a member which is a pointer point to my allocate
memory ( no ref count).
ex:
---
typedef struct {
   PyObject_HEAD
   /* Type-specific fields go here. */
   mytype *p;
} noddy_NoddyObject;

And i write dealloc function like this:
---
static void
Noddy_dealloc(Noddy* self)
{
   delete p;
   self-ob_type-tp_free((PyObject*)self);
}

And I found it's strange that it didn't call dealloc when there is no
reference.

ex:
a = Noddy()
b = a
del a
del b
# i think there is no ref to the object, and it should call dealloc
but it didn't call!!!

could anyone tell me what happened?



Hi Shen,
I'm no expert on Python memory management, but since no once else has  
answered your question I'll tell you what I *think* is happening.


Python doesn't delete objects as soon as they're dereferenced. It  
merely marks them as safe for garbage collection (GC). If GC never  
happens (and it might not in a small test program), your dealloc  
function won't run.


Now some more knowledgeable person will probably correct me. =)

You can control the garbage collector manually with Python's gc  
module. You will probably never want to control the garbage collector  
manually in ordinary programs, but it can be useful for testing.



Hope this helps
Philip

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


Re: dealloc function in python extend c module

2009-07-02 Thread Gabriel Genellina

En Thu, 02 Jul 2009 14:22:53 -0300, Philip Semanchuk
phi...@semanchuk.com escribió:

On Jul 2, 2009, at 2:11 AM, Shen, Yu-Teh wrote:

I create my extend type something like  
http://www.python.org/doc/current/extending/newtypes.html.

And my type has a member which is a pointer point to my allocate
memory ( no ref count).
ex:
---
typedef struct {
   PyObject_HEAD
   /* Type-specific fields go here. */
   mytype *p;
} noddy_NoddyObject;

And i write dealloc function like this:
---
static void
Noddy_dealloc(Noddy* self)
{
   delete p;
   self-ob_type-tp_free((PyObject*)self);
}

And I found it's strange that it didn't call dealloc when there is no
reference.


And you set the tp_dealloc field in the type structure to Noddy_dealloc,
did you?


ex:
a = Noddy()
b = a
del a
del b
# i think there is no ref to the object, and it should call dealloc
but it didn't call!!!


You can use sys.getrefcount(a) to check how many references it has.
Remember that it returns one more than the value you expect (because the
function itself holds a temporary reference to its argument)


Hi Shen,
I'm no expert on Python memory management, but since no once else has  
answered your question I'll tell you what I *think* is happening.


Python doesn't delete objects as soon as they're dereferenced.


Nope. CPython *does* destroy objects as soon as their reference count
reaches zero. It does not rely on garbage collection for that.

It merely marks them as safe for garbage collection (GC). If GC never  
happens (and it might not in a small test program), your dealloc  
function won't run.


The garbage collector is only used to recover memory from object cycles
(in the CPython implementation; Jython *does* use garbage collection for
normal object destruction too)


Now some more knowledgeable person will probably correct me. =)


And now some more knowledgeable person will probably correct me. =)

--
Gabriel Genellina

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


Re: dealloc function in python extend c module

2009-07-02 Thread Philip Semanchuk


On Jul 2, 2009, at 9:28 PM, Gabriel Genellina wrote:


En Thu, 02 Jul 2009 14:22:53 -0300, Philip Semanchuk
phi...@semanchuk.com escribió:


Hi Shen,
I'm no expert on Python memory management, but since no once else  
has answered your question I'll tell you what I *think* is happening.


Python doesn't delete objects as soon as they're dereferenced.


Nope. CPython *does* destroy objects as soon as their reference count
reaches zero. It does not rely on garbage collection for that.

It merely marks them as safe for garbage collection (GC). If GC  
never happens (and it might not in a small test program), your  
dealloc function won't run.


The garbage collector is only used to recover memory from object  
cycles
(in the CPython implementation; Jython *does* use garbage collection  
for

normal object destruction too)


Now some more knowledgeable person will probably correct me. =)


Wow, that last part was the only part I got right. =) Thanks for  
straightening me out, Gabriel.




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