473,382 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Problems when destroy object which is referenced member objects

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
1 2255

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: claire.bell1 | last post by:
Hi, Im having problems initialising class member objects in the class's constructor e.g. My program isnt about monkeys, but there is too much code to post here. class monkey { private: int...
15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
9
by: Joost Ronkes Agerbeek | last post by:
Are member objects constructed before the body of the constructor executes? Consider the following example. Is this okay or is it possible that _bar will be created after the call to DoBar()? ...
5
by: Paul Wilkinson | last post by:
How can I write a method that is called when an object goes out of scope? I've looked all over, so far I've only found discussions of how to write methods that don't = )
6
by: Belebele | last post by:
Suppose I want to use some object's member function as the action passed to a for_each call: class A { ... public: void foo(int ); }; A a;
0
by: tom arnall | last post by:
> George, did you ever put your object dumper on the internet? tom arnall north spit, ca
12
by: Janaka Perera | last post by:
Hi All, We have done a object oriented design for a system which will create a class multiply inherited by around 1000 small and medium sized classes. I would be greatful if you can help me...
5
by: mackenzie | last post by:
Hello, I am looking for a little bit of help. I am trying to create a dynamically allocated object which contains one or more objects of type boost::pool<>. I get a compiler error when an object...
3
by: stefven blonqhern | last post by:
Hello, something of a C++ beginner here with a (possibly) (really) bad question: I want to pass a reference of an object from one of the object's member functions. Is this possible? so...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.