Problems when destroy object which is referenced member objects ['LBBW': checked]
Holger Joukl
Holger.Joukl at LBBW.de
Thu Feb 19 09:36:07 EST 2004
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?
--
http://mail.python.org/mailman/listinfo/python-list
Hi,
the refcount _is_ actually decreased by python, as you can see if you add
some code:
Python 2.3.3 (#12, Feb 19 2004, 11:42:09)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> 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
... def __del__(self):
... print "TestB.__del__"
...
>>> testB = TestB()
init TestB
init TestA
>>> print testB
<__main__.TestB instance at 0x1a75a8>
>>> import sys
>>> print sys.getrefcount(testB)
3
>>> # 1 more than expected because of testB given as function argument
... # This means after the return of getrefcount(), 2 references on
... # <__main__.TestB instance at 0x1a75a8> exist
... del testB
>>> # refcount is decreased but does not reach zero!
As the reference count does not reach zero, the object is not destroyed
(->destructor is not invoked, memory is not deallocated, but name
testB is destroyed, so it is not accessible any more).
You have a circular reference here, see python documention.
Der Inhalt dieser E-Mail ist vertraulich. Falls Sie nicht der angegebene
Empfänger sind oder falls diese E-Mail irrtümlich an Sie adressiert wurde,
verständigen Sie bitte den Absender sofort und löschen Sie die E-Mail
sodann. Das unerlaubte Kopieren sowie die unbefugte Übermittlung sind nicht
gestattet. Die Sicherheit von Übermittlungen per E-Mail kann nicht
garantiert werden. Falls Sie eine Bestätigung wünschen, fordern Sie bitte
den Inhalt der E-Mail als Hardcopy an.
The contents of this e-mail are confidential. If you are not the named
addressee or if this transmission has been addressed to you in error,
please notify the sender immediately and then delete this e-mail. Any
unauthorized copying and transmission is forbidden. E-Mail transmission
cannot be guaranteed to be secure. If verification is required, please
request a hard copy version.
More information about the Python-list
mailing list