Bug in __del__ function

D

David MacQuigg

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
.... Deleting instance: <__main__.Cat object at 0x00A11F70>
Cat:0 Mammal:0 Animal:0
' Cat:1 Mammal:0 Animal:0' ### Count is wrong.' Cat:0 Mammal:0 Animal:0' ### Count is right.

### END ###
 
H

Heather Coppersmith

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 ]
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
 
D

David MacQuigg

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 ]
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.
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top