By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,932 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

Problems when destroy object which is referenced member objects

P: n/a
Hi.
Please check this simple test code.

---------------------
class TestA:
def __init__(self):
print "init TestA"
def __del__(self):
print "del TestA"
def SetEvent(self, event):
self.event = event

class TestB:
def __init__(self):
print "init TestB"
self.testA = TestA()
self.testA.SetEvent(self.Test)
def __del__(self):
print "del TestB"
def Test(self):
pass

testB = TestB()
del testB
---------------------

I want to run this code that destroy testB and testB's member object
testA. But no object destroy. I know testA increase testB's reference
count, but why wouldn't python decrease testB's reference count? Had I
do wrong something?
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Minkyu Kim" <le*****@lycos.co.kr> wrote in message
news:a9**************************@posting.google.c om...
class TestA:
def __init__(self):
print "init TestA"
def __del__(self):
print "del TestA"
def SetEvent(self, event):
self.event = event

class TestB:
def __init__(self):
print "init TestB"
self.testA = TestA()
self.testA.SetEvent(self.Test)
def __del__(self):
print "del TestB"
def Test(self):
pass

testB = TestB()
del testB
---------------------

I want to run this code that destroy testB and testB's member object
testA. But no object destroy. I know testA increase testB's reference
count, but why wouldn't python decrease testB's reference count? Had I
do wrong something?


You created a circular reference loop of objects with __del__ methods.
Without adding 'del testB.testA' to break the loop, before the testB
deletion, the reference counts never go to 0.

I believe the gc garbage collector will also leave the loop alone because
of the __del__ methods. Trying to watch gc in action stops it (!) because
__del__ methods can do (and have done) things that mess up the gc process,
so gc no longer tries when they are present. So break the loop as
indicated above.

Terry J. Reedy


Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.