By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,445 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,445 IT Pros & Developers. It's quick & easy.

Releasing weakrefed objects immediately?

P: n/a
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.