dealloc function in python extend c module

Discussion in 'Python' started by Shen, Yu-Teh, Jul 2, 2009.

  1. Shen, Yu-Teh

    Shen, Yu-Teh Guest

    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?

    Thanks a lot!!
     
    Shen, Yu-Teh, Jul 2, 2009
    #1
    1. Advertising

  2. 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
     
    Philip Semanchuk, Jul 2, 2009
    #2
    1. Advertising

  3. En Thu, 02 Jul 2009 14:22:53 -0300, Philip Semanchuk
    <> 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
     
    Gabriel Genellina, Jul 3, 2009
    #3
  4. On Jul 2, 2009, at 9:28 PM, Gabriel Genellina wrote:

    > En Thu, 02 Jul 2009 14:22:53 -0300, Philip Semanchuk
    > <> 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.
     
    Philip Semanchuk, Jul 3, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. B. Gandhi

    Memory Alloc/Dealloc query

    B. Gandhi, Sep 23, 2003, in forum: C Programming
    Replies:
    6
    Views:
    621
    Peter Shaggy Haywood
    Sep 25, 2003
  2. delete vs dealloc

    , Dec 20, 2006, in forum: C++
    Replies:
    3
    Views:
    1,491
    Daniel T.
    Dec 20, 2006
  3. Replies:
    6
    Views:
    563
  4. §ä´M¦Û¤vªº¤@¤ù¤Ñ

    python extend c++ module

    §ä´M¦Û¤vªº¤@¤ù¤Ñ, Jun 29, 2009, in forum: Python
    Replies:
    0
    Views:
    314
    §ä´M¦Û¤vªº¤@¤ù¤Ñ
    Jun 29, 2009
  5. Shen, Yu-Teh

    python extend c++ module

    Shen, Yu-Teh, Jun 30, 2009, in forum: Python
    Replies:
    0
    Views:
    271
    Shen, Yu-Teh
    Jun 30, 2009
Loading...

Share This Page