Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Weakref.ref callbacks and eliminating __del__ methods

Reply
Thread Tools

Weakref.ref callbacks and eliminating __del__ methods

 
 
Mike C. Fletcher
Guest
Posts: n/a
 
      01-23-2005
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

 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      01-23-2005
Mike C. Fletcher <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Mike C. Fletcher
Guest
Posts: n/a
 
      01-24-2005
Alex Martelli wrote:

>Mike C. Fletcher <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
__del__ methods Jason Baker Python 11 07-20-2008 11:30 AM
registering class methods as C style callbacks noone@all.com C++ 13 07-24-2006 12:09 PM
passing c++ methods as c-callbacks zholthran@web.de C++ 9 04-26-2006 07:11 PM
wxPython: accessing wxFrame methods in __del__ fails Alex VanderWoude Python 2 03-25-2005 05:59 AM
Re: Weakref.ref callbacks and eliminating __del__ methods Mike C. Fletcher Python 0 01-24-2005 03:35 PM



Advertisments