Bug in __del__ function

Discussion in 'Python' started by David MacQuigg, Jun 24, 2004.

  1. I'm __del__() to decrement a count of instances of a class whenever an
    instance is deleted, and I'm getting very erratic behavior. This is
    in CPython, which I am told has no problem with reference counts
    getting out-of-sync with the deletion of instances. The behavior of
    __del__() seems to be very sensitive to the exact sequence of
    commands.

    The example below is a repeatable test case. It might be possible to
    simplify this further, but at this point, I haven't a clue as to why
    it works with some sequences of commands, but not others.

    I'm also not able to repeat the problem by putting the commands in the
    file, and just running the file. Adding the necessary 'print'
    keywords makes the problem go away. This could be just a problem in
    IDLE, but unless I'm sure of that, I don't want to be using __del__ in
    my programs.

    Is this a bug, or have I misunderstood the use of __del__?

    -- Dave

    ### test__del__.py

    class Animal(object):
    _count = 0
    def __init__(self):
    print "Creating instance:", self
    self.__class__._count += 1
    def __del__(self):
    print "Deleting instance:", self
    self.__class__._count -= 1
    print countem()

    class Mammal(Animal):
    _count = 0

    class Cat(Mammal):
    _count = 0

    def countem():
    str = ''
    for cls in Cat.__mro__[:-1]:
    str += '%9s:%s' % (cls.__name__, cls._count)
    return str

    import sys
    def rc(obj):
    print "sys.getrefcount(obj):", sys.getrefcount(obj)

    ### Run the above file in IDLE:

    Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)]
    on win32
    ....
    IDLE 1.0.3
    ....
    >>> ================================ RESTART ===================
    >>>
    >>> cat1 = Cat()

    Creating instance: <__main__.Cat object at 0x00A11F70>
    >>> cat2 = cat1
    >>> countem()

    ' Cat:1 Mammal:0 Animal:0'
    >>> rc(cat1)

    sys.getrefcount(obj): 5
    >>> rc(cat2)

    sys.getrefcount(obj): 5
    >>> del cat1
    >>> cat2

    <__main__.Cat object at 0x00A11F70>
    >>> rc(cat2)

    sys.getrefcount(obj): 5 ### Should be one less.
    >>> del cat2 ### Should delete instance.
    >>> countem()

    Deleting instance: <__main__.Cat object at 0x00A11F70>
    Cat:0 Mammal:0 Animal:0
    ' Cat:1 Mammal:0 Animal:0' ### Count is wrong.
    >>> countem()

    ' Cat:0 Mammal:0 Animal:0' ### Count is right.

    ### END ###
    David MacQuigg, Jun 24, 2004
    #1
    1. Advertising

  2. On Wed, 23 Jun 2004 18:07:50 -0700,
    David MacQuigg <> wrote:

    > The example below is a repeatable test case. It might be possible to
    > simplify this further, but at this point, I haven't a clue as to why
    > it works with some sequences of commands, but not others.


    > I'm also not able to repeat the problem by putting the commands in the
    > file, and just running the file. Adding the necessary 'print'
    > keywords makes the problem go away. This could be just a problem in
    > IDLE, but unless I'm sure of that, I don't want to be using __del__ in
    > my programs.


    > Is this a bug, or have I misunderstood the use of __del__?


    [ code / interaction mostly snipped ]

    >>>> del cat1
    >>>> cat2

    > <__main__.Cat object at 0x00A11F70>
    >>>> rc(cat2)

    > sys.getrefcount(obj): 5 ### Should be one less.


    I'm not sure about that. The interactive prompt keeps a reference to
    the last thing it printed, which in this case was cat2. Perhaps IDLE
    has similar behavior? That would also explain why adding extra print
    statements and/or running from a file makes the problem go away.

    Regards,
    Heather

    --
    Heather Coppersmith
    That's not right; that's not even wrong. -- Wolfgang Pauli
    Heather Coppersmith, Jun 24, 2004
    #2
    1. Advertising

  3. On 23 Jun 2004 21:22:59 -0400, Heather Coppersmith <>
    wrote:

    >On Wed, 23 Jun 2004 18:07:50 -0700,
    >David MacQuigg <> wrote:
    >
    >> The example below is a repeatable test case. It might be possible to
    >> simplify this further, but at this point, I haven't a clue as to why
    >> it works with some sequences of commands, but not others.

    >
    >> I'm also not able to repeat the problem by putting the commands in the
    >> file, and just running the file. Adding the necessary 'print'
    >> keywords makes the problem go away. This could be just a problem in
    >> IDLE, but unless I'm sure of that, I don't want to be using __del__ in
    >> my programs.

    >
    >> Is this a bug, or have I misunderstood the use of __del__?

    >
    >[ code / interaction mostly snipped ]
    >
    >>>>> del cat1
    >>>>> cat2

    >> <__main__.Cat object at 0x00A11F70>
    >>>>> rc(cat2)

    >> sys.getrefcount(obj): 5 ### Should be one less.

    >
    >I'm not sure about that. The interactive prompt keeps a reference to
    >the last thing it printed, which in this case was cat2. Perhaps IDLE
    >has similar behavior? That would also explain why adding extra print
    >statements and/or running from a file makes the problem go away.


    Wow. This explains everything. The erratic behavior is all dependent
    on what the interpreter sees as the current _ (underscore) reference.
    Simply printing a value changes that reference and frees the deleted
    object.

    >>> del cat2 ### nothing happens, because _ still references the object.
    >>> _

    <__main__.Cat object at 0x00A11F70>
    >>> 2 ### changes _ and immediately frees the object.

    Deleting instance: <__main__.Cat object at 0x00A11F70>
    Cat:0 Mammal:0 Animal:0
    2 ### The deletion apparently occurs before the command finishes.
    >>>


    This will make a good example for the "Gotcha" section in my OOP
    chapter. Thanks for your help.

    -- Dave
    David MacQuigg, Jun 24, 2004
    #3
    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. Jane Austine
    Replies:
    2
    Views:
    337
    Steven Taschuk
    Jul 2, 2003
  2. Jane Austine
    Replies:
    1
    Views:
    454
    Erik Max Francis
    Jul 2, 2003
  3. seth
    Replies:
    0
    Views:
    340
  4. Kepes Krisztian

    Java final vs Py __del__

    Kepes Krisztian, Nov 27, 2003, in forum: Python
    Replies:
    2
    Views:
    349
    Jay O'Connor
    Nov 27, 2003
  5. Baoqiu Cui
    Replies:
    4
    Views:
    507
    Baoqiu Cui
    Jan 31, 2005
Loading...

Share This Page