470,594 Members | 1,395 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

Bug in __del__ function

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 ###

Jul 18 '05 #1
2 1330
On Wed, 23 Jun 2004 18:07:50 -0700,
David MacQuigg <dm*@gain.com> 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
Jul 18 '05 #2
On 23 Jun 2004 21:22:59 -0400, Heather Coppersmith <me@privacy.net>
wrote:
On Wed, 23 Jun 2004 18:07:50 -0700,
David MacQuigg <dm*@gain.com> 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

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Kepes Krisztian | last post: by
13 posts views Thread by Emmanuel | last post: by
reply views Thread by John Hunter | last post: by
1 post views Thread by schwerdy | last post: by
2 posts views Thread by flupke | last post: by
8 posts views Thread by Gregor Horvath | last post: by
10 posts views Thread by Max Yuzhakov | last post: by
1 post views Thread by Erwan Adam | last post: by
6 posts views Thread by George Sakkis | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.