473,796 Members | 2,520 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bug in __del__ function

I'm __del__() to decrement a count of instances of a class whenever an
instance is deleted, and I'm getting very erratic behavior. This is
in CPython, which I am told has no problem with reference counts
getting out-of-sync with the deletion of instances. The behavior of
__del__() seems to be very sensitive to the exact sequence of
commands.

The example below is a repeatable test case. It might be possible to
simplify this further, but at this point, I haven't a clue as to why
it works with some sequences of commands, but not others.

I'm also not able to repeat the problem by putting the commands in the
file, and just running the file. Adding the necessary 'print'
keywords makes the problem go away. This could be just a problem in
IDLE, but unless I'm sure of that, I don't want to be using __del__ in
my programs.

Is this a bug, or have I misunderstood the use of __del__?

-- Dave

### test__del__.py

class Animal(object):
_count = 0
def __init__(self):
print "Creating instance:", self
self.__class__. _count += 1
def __del__(self):
print "Deleting instance:", self
self.__class__. _count -= 1
print countem()

class Mammal(Animal):
_count = 0

class Cat(Mammal):
_count = 0

def countem():
str = ''
for cls in Cat.__mro__[:-1]:
str += '%9s:%s' % (cls.__name__, cls._count)
return str

import sys
def rc(obj):
print "sys.getrefcoun t(obj):", sys.getrefcount (obj)

### Run the above file in IDLE:

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)]
on win32
....
IDLE 1.0.3
....
=============== =============== == RESTART =============== ====

cat1 = Cat() Creating instance: <__main__.Cat object at 0x00A11F70> cat2 = cat1
countem() ' Cat:1 Mammal:0 Animal:0' rc(cat1) sys.getrefcount (obj): 5 rc(cat2) sys.getrefcount (obj): 5 del cat1
cat2 <__main__.Cat object at 0x00A11F70> rc(cat2) sys.getrefcount (obj): 5 ### Should be one less. del cat2 ### Should delete instance.
countem() Deleting instance: <__main__.Cat object at 0x00A11F70>
Cat:0 Mammal:0 Animal:0
' Cat:1 Mammal:0 Animal:0' ### Count is wrong. countem()

' Cat:0 Mammal:0 Animal:0' ### Count is right.

### END ###

Jul 18 '05 #1
2 1435
On Wed, 23 Jun 2004 18:07:50 -0700,
David MacQuigg <dm*@gain.com > wrote:
The example below is a repeatable test case. It might be possible to
simplify this further, but at this point, I haven't a clue as to why
it works with some sequences of commands, but not others. I'm also not able to repeat the problem by putting the commands in the
file, and just running the file. Adding the necessary 'print'
keywords makes the problem go away. This could be just a problem in
IDLE, but unless I'm sure of that, I don't want to be using __del__ in
my programs. Is this a bug, or have I misunderstood the use of __del__?


[ code / interaction mostly snipped ]
del cat1
cat2 <__main__.Cat object at 0x00A11F70> rc(cat2)

sys.getrefcount (obj): 5 ### Should be one less.


I'm not sure about that. The interactive prompt keeps a reference to
the last thing it printed, which in this case was cat2. Perhaps IDLE
has similar behavior? That would also explain why adding extra print
statements and/or running from a file makes the problem go away.

Regards,
Heather

--
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli
Jul 18 '05 #2
On 23 Jun 2004 21:22:59 -0400, Heather Coppersmith <me@privacy.net >
wrote:
On Wed, 23 Jun 2004 18:07:50 -0700,
David MacQuigg <dm*@gain.com > wrote:
The example below is a repeatable test case. It might be possible to
simplify this further, but at this point, I haven't a clue as to why
it works with some sequences of commands, but not others.

I'm also not able to repeat the problem by putting the commands in the
file, and just running the file. Adding the necessary 'print'
keywords makes the problem go away. This could be just a problem in
IDLE, but unless I'm sure of that, I don't want to be using __del__ in
my programs.

Is this a bug, or have I misunderstood the use of __del__?


[ code / interaction mostly snipped ]
> del cat1
> cat2

<__main__.Cat object at 0x00A11F70>
> rc(cat2)

sys.getrefcount (obj): 5 ### Should be one less.


I'm not sure about that. The interactive prompt keeps a reference to
the last thing it printed, which in this case was cat2. Perhaps IDLE
has similar behavior? That would also explain why adding extra print
statements and/or running from a file makes the problem go away.


Wow. This explains everything. The erratic behavior is all dependent
on what the interpreter sees as the current _ (underscore) reference.
Simply printing a value changes that reference and frees the deleted
object.
del cat2 ### nothing happens, because _ still references the object.
_ <__main__.Cat object at 0x00A11F70> 2 ### changes _ and immediately frees the object. Deleting instance: <__main__.Cat object at 0x00A11F70>
Cat:0 Mammal:0 Animal:0
2 ### The deletion apparently occurs before the command finishes.


This will make a good example for the "Gotcha" section in my OOP
chapter. Thanks for your help.

-- Dave

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1645
by: Kepes Krisztian | last post by:
Hi ! I very wonder, when I get exp. in java with GC. I'm Delphi programmer, so I get used to destructorin objects. In Java the final method is not same, but is like to destructor (I has been think...). And then I try with some examples, I see, that the Java GC is
13
2038
by: Emmanuel | last post by:
Hi, I run across this problem, and couldn't find any solution (python 2.2.2) : Code : =========== from __future__ import generators >>> class titi:
0
1155
by: John Hunter | last post by:
I have a class that uses some extension code I have written and I am trying to track down some memory leaks, which I presume to be in my extension code. The class is question is in a python module, not extension code. I notice some strange behavior; perhaps a guru can give me a pointer about what this may mean. If I define the __del__ method in the class C class C: ...lots of other stuff...
1
2028
by: schwerdy | last post by:
Hello developers! I'm using Python 2.3.4 under debian Sarge and want to write a small logger class. My source code reads: #*************************************************** import sys, time from fcntl import * class Log(object): """
2
3329
by: flupke | last post by:
Hi, i have a class and a class attribute log which is a logger object. In the __del__() function i want to log a message but it fails even if i use self.__class__.log. The error i get is this: Traceback (most recent call last): File "C:\Python24\lib\logging\__init__.py", line 712, in emit self.stream.write(fs % msg)
8
8379
by: Gregor Horvath | last post by:
Hi, I do not understand why __del__ does not get executed in the following example. test.py: #!/usr/bin/python class A(object): def __init__(self):
10
3196
by: Max Yuzhakov | last post by:
Hello! It is correct behaviour for python to call __del__ on some identity of a class object more than once? In brief I shall describe a situation. Sorry for my english. For debugin purposes I'm put in my module global counters for counting __init__ and __del__ calls.
1
1491
by: Erwan Adam | last post by:
Hello all, Can someone reproduce this bug ... I use : python Python 2.4.3 (#2, Sep 18 2006, 21:07:35) on linux2 Type "help", "copyright", "credits" or "license" for more information. First test :
6
2876
by: George Sakkis | last post by:
I'm baffled with a situation that involves: 1) an instance of some class that defines __del__, 2) a thread which is created, started and referenced by that instance, and 3) a weakref proxy to the instance that is passed to the thread instead of 'self', to prevent a cyclic reference. This probably sounds like gibberish so here's a simplified example: ==========================================
0
10231
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...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
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
9054
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...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5443
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
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
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
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.