473,788 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SetE vent(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 2277

"Minkyu Kim" <le*****@lycos. co.kr> wrote in message
news:a9******** *************** ***@posting.goo gle.com...
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.SetE vent(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
2778
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 age;
15
2889
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 int member: MyClass::myStaticMemberInt = 99;
9
1363
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()? class Bar { public: Set(int i) { _i = i; }
5
4853
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
4034
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
266
by: tom arnall | last post by:
> George, did you ever put your object dumper on the internet? tom arnall north spit, ca
12
2095
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 with the following: Can this create any problems with GNU g++ compilation, linking etc? What would be the impact to the performance of the system? Anything else we have to consider?
5
2850
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 of this type is contained in my class and am not sure why. To be honest I have a little but not a lot of experience with templates and it could simply be obvious to a more experienced template user; however, the answer escapes me. Here is a...
3
1285
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 something like this (i've simplified what I'm doing): MyFuntion(TestClass& some_object) {
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.