[ python-Bugs-1553819 ] Class instance apparently not destructed when expected

SourceForge.net noreply at sourceforge.net
Fri Sep 8 05:09:51 CEST 2006


Bugs item #1553819, was opened at 2006-09-06 20:26
Message generated for change (Comment added) made by nnorwitz
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1553819&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Peter Donis (peterdonis)
Assigned to: Nobody/Anonymous (nobody)
Summary: Class instance apparently not destructed when expected

Initial Comment:
When an instance variable of a class with the same 
name as a class variable in a base class is assigned 
a value (making the class variable of the base class 
invisible), the class instance does not appear to be 
destructed when it should.

Here is the simplest test script I've been able to 
come up with that reproduces the error, along with 
its output when run from a shell prompt. I've 
included the dir() commands to make sure that the 
variable referencing the class instance is in fact 
deleted in both cases. As you can see, the instance 
of the base class gets destructed as expected, but 
the instance of the derived class does not.

--- Test script ---

#!/usr/bin/env python
# Test script to see when objects are freed

class Test(object):
    
    testfield = None
    
    def __init__(self):
        print "Initializing test object."
    
    def __del__(self):
        print "Freeing test object."

class Test2(Test):
    
    def __init__(self):
        # This masks Test.testfield
        self.testfield = self.meth
        Test.__init__(self)
    
    def meth(self):
        pass

print dir()
t = Test()
print dir()
del t
print dir()
t2 = Test2()
print dir()
del t2
print dir()

--- Output ---

$ python deltest.py
['Test', 'Test2', '__builtins__', '__doc__', '__file__', '__name__', 'func']
Initializing test object.
['Test', 'Test2', '__builtins__', '__doc__', '__file__', '__name__', 'func', 't']
Freeing test object.
['Test', 'Test2', '__builtins__', '__doc__', '__file__', '__name__', 'func']
Initializing test object.
['Test', 'Test2', '__builtins__', '__doc__', '__file__', '__name__', 'func', 't2']
['Test', 'Test2', '__builtins__', '__doc__', '__file__', '__name__', 'func']


----------------------------------------------------------------------

>Comment By: Neal Norwitz (nnorwitz)
Date: 2006-09-07 20:09

Message:
Logged In: YES 
user_id=33168

Michael: coffee, no, sleep, yes. :-)

----------------------------------------------------------------------

Comment By: Peter Donis (peterdonis)
Date: 2006-09-07 20:05

Message:
Logged In: YES 
user_id=1592444

mwh's comment is correct. See attached script deltest2.py, 
showing the contents of gc.garbage and how the reference 
cycle can be cleared (script output shown below). Note that 
I eliminated the base class and the masking of its class 
field; that was a red herring. My only other question is 
whether it might be worthwhile to add a sentence or two to 
the description of bound method objects in the 
documentation to make it clearer that you're creating a 
reference to the class instance.

--- script output ---

$ python deltest2.py
['Test', '__builtins__', '__doc__', '__file__', '__name__', 
'gc']
Initializing test object.
['Test', '__builtins__', '__doc__', '__file__', '__name__', 
'gc', 't']
['Test', '__builtins__', '__doc__', '__file__', '__name__', 
'gc']
[]
gc: uncollectable <Test 0xb7c3f34c>
gc: uncollectable <dict 0xb7c4c1c4>
gc: uncollectable <instancemethod 0xb7cb86bc>
3
[<__main__.Test object at 0xb7c3f34c>, {'testfield': <bound 
method Test.meth of <__main__.Test object at 0xb7c3f34c>>}, 
<bound method Test.meth of <__main__.Test object at 
0xb7c3f34c>>]
[]
0
['Test', '__builtins__', '__doc__', '__file__', '__name__', 
'g', 'gc']
Freeing test object.


----------------------------------------------------------------------

Comment By: Michael Hudson (mwh)
Date: 2006-09-07 07:06

Message:
Logged In: YES 
user_id=6656

It's a reference cycle featuring an object with a __del__ --> it ends up in 
gc.garbage.

More coffee for Neal?

----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2006-09-07 00:19

Message:
Logged In: YES 
user_id=33168

The attached variant puts this into a function and shows ref
leaks.  It requires a debug build.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1553819&group_id=5470


More information about the Python-bugs-list mailing list