473,383 Members | 1,725 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,383 software developers and data experts.

Weakref.ref callbacks and eliminating __del__ methods

I'm looking at rewriting parts of Twisted and TwistedSNMP to eliminate
__del__ methods (and the memory leaks they create). Looking at the docs
for 2.3's weakref.ref, there's no mention of whether the callbacks are
held with a strong reference. My experiments suggest they are not...
i.e. I'm trying to use this pattern:

class Closer( object ):
"""Close the OIDStore (without a __del__)"""
def __init__( self, btree ):
"""Initialise the closer object"""
self.btree = btree
def __call__( self, oldObject=None ):
"""Regular call via self.close or weakref deref"""
if self.btree:
self.btree.close()
self.btree = None
class BSDOIDStore(oidstore.OIDStore):
def __init__( self, filename, OIDs = None ):
"""Initialise the storage with appropriate OIDs"""
self.btree = self.open( filename )
self.update( OIDs )
self.close = Closer( self.btree )
weakref.ref( self, self.close )

but the self.close reference in the instance is going away *before* the
object is called.

So, this approach doesn't *seem* to work (the Closer doesn't get
called), so I can gather that the callbacks don't get incref'd (or they
get decref'd during object deletion).

I can work around it in this particular case by defining a __del__ on
the Closer, but that just fixes this particular instance (and leaves
just as many __del__'s hanging around). I'm wondering if there's a
ready recipe that can *always* replace a __del__'s operation?

I know I heard a rumour somewhere about Uncle Timmy wanting to eliminate
__del__ in 2.5 or thereabouts, so I gather there must be *some* way of
handling the problem generally. The thing is, weakref callbacks trigger
*after* the object is deconstructed, while __del__ triggers before...
must be something clever I'm missing.

Throw an old doggie a bone?
Mike

________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

Jul 18 '05 #1
2 2056
Mike C. Fletcher <mc******@rogers.com> wrote:
weakref.ref( self, self.close )

but the self.close reference in the instance is going away *before* the
object is called.


Uh -- what's holding on to this weakref.ref instance? I guess the
weakreference _itself_ is going away right after being created...
Alex
Jul 18 '05 #2
Alex Martelli wrote:
Mike C. Fletcher <mc******@rogers.com> wrote:
weakref.ref( self, self.close )

but the self.close reference in the instance is going away *before* the
object is called.


Uh -- what's holding on to this weakref.ref instance? I guess the
weakreference _itself_ is going away right after being created...

You know, you're right. I'd been thinking (not-very-clearly) that
registering the callback would keep the reference alive until it was
called, guess I'm too used to PyDispatcher's operation. Urgh, that's
seriously annoying, requires storing the callback somewhere external.

Back to __del__ I suppose.

Thanks Alex,
Mike
________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

Jul 18 '05 #3

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

Similar topics

6
by: Peter Abel | last post by:
I have an application, which is an instance of a class with a deeply nested object hierarchy. Among others one method will be executed as a thread, which can be stopped. Everything works fine...
1
by: Mathias Mamsch | last post by:
Hi, I have some confusion concerning the weakref module. I am trying to save a weak reference to a bound member function of a class instance for using it as a callback function. But I always...
13
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:
4
by: Baoqiu Cui | last post by:
Today I was playing with a small Python program using Python 2.4 on Cygwin (up-to-date version, on Windows XP), but ran into a strange error on the following small program (named bug.py): ...
2
by: Alex VanderWoude | last post by:
I am attempting to save my window's size and position when it closes. So I figured I'd put some code in the __del__() method: from wxPython import * class MyWindow(wxFrame): def __init__(self,...
13
by: Torsten Bronger | last post by:
Hallöchen! When my __del__ methods are called because the program is being terminated, I experience difficulties in calling functions that I need for a clean shutdown of my instances. So far,...
3
by: Odalrick | last post by:
I'm trying to write a simple game and decided I need an eventmanager. <code> import weakref from collections import defaultdict class _EventManager( object ): def __init__( self ):...
6
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...
11
by: Jason Baker | last post by:
I have a class that I need to do some finalization on when it dies. I know I can use the __del__ method, but I seem to recall that it impedes garbage collection. Is this the case? (keep in...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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.