469,631 Members | 1,043 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

Releasing weakrefed objects immediately?

Hi all,

I've been toying with an idea of dependency-based coding, in which you
construct a graph of code dependencies. Say, you have independent
variables 'a' and 'b', and a function f(x,y). Now, you can define that a,b
are dependencies for f(a,b). Whenever a or b are changed, f(a,b) is marked
dirty, and if its value is requested, all the dirty dependencies are
recomputed to evaluate the value of f(a,b). Basically the same idea as in
makefiles.

I also want to have listener objects, so that I could make a GUI text box
depend on f(a,b) so that its value is updated whenever a or b are
modified. So far so good, and all of this I have already implemented. My
problem, however, is that I want to be able to destroy the listener
objects at will, for example when a window is closed. Unfortunately, even
though the only real reference to an object is destroyed, the weak
references seem to keep the listener alive (or the garbage collector won't
kick in for some other reason, even though explicitly called).

Here is the code snippet for my test case:

def testlistener(self):
self.input1 = Dep(value=3,id="input1")
self.input2 = Dep(value=4,id="input2")
self.output1 = Dep((self.input1,self.input2),lambda x,y: x*y,
id="output1")
self.listenerResult = 0
def listenerFunc(self,x):
self.listenerResult = x
self.listener1 = Dep((self.output1,),
lambda x: listenerFunc(self,x),
id="listener1",
isListener=True)
self.assertEqual(self.listenerResult,12)
self.input1.value = 5
self.assertEqual(self.listenerResult,20)
#self.listener1._code = lambda x: x
del self.listener1
gc.collect()
self.input1.value = 4
self.assertEqual(self.listenerResult,20)

The self.listenerResults always is 16 in the final asserEqual, despite
that I have deleted the listener. If I "neuter" the listener by
uncommenting the commented line, I get the right result. So, how could I
make sure that the listener is no longer called when the only strong ref
is removed?

The actual depcode class with the complete testcase and another example
using scipy and matplotlib is available at
http://www.mairas.net/tmp/depcode/ . Note that it is very much code in
progress at the moment.

Thanks for any hints,

Matti Airas

Oct 20 '06 #1
0 887

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by lylefair | last post: by
1 post views Thread by Chris | last post: by
8 posts views Thread by One Handed Man [ OHM# ] | last post: by
2 posts views Thread by M.Ob | last post: by
2 posts views Thread by Pete Smith | last post: by
12 posts views Thread by iker.arizmendi | last post: by
reply views Thread by 062699 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.