weak listener in GWT?

213 views
Skip to first unread message

Ed

unread,
Mar 25, 2009, 10:59:36 AM3/25/09
to Google Web Toolkit
He all,

Please a bit of advice on the following?

In Java you can use the WeakReference object to work with so called
weak Listeners. More details on this can be found here for example:
http://www.javalobby.org/java/forums/t19468.html
or: http://www.ibm.com/developerworks/java/library/j-jtp11225/

I would love to do something similar but WeakReference isn't supported
in GWT. How do others deal with it ?

My problem: Suppose the following:
ViewCategory is created and adds a property listener to a global Model
to listen to Member name changes.
The view isn't needed anymore and as such his instance reference made
null in the controller where it's used.
The next time this view is needed, a new ViewCategory instance is
created, a listener is registered and the view is shown. We assume
that the old instance was garbage collected -> wrong assumption :(
Suppose that I forget to unregister the listener -> memory leak.....
The model keeps an association to the nested listener in the
ViewCategory instance such that this object and his nested objects
stay in memory.

Ofcourse you then say:" then clean this properly"... Yep... I do my
best, but in a complex situation with many requirements and things
going from left to right, it can happen that this is forgotten.

What would be nice: weak listeners, like Swing has, such that when he
ViewCategory isn't associated anymore, it's removed and as such also
his listener from the global model.

How do I realize something like this ?

-- Ed

gregor

unread,
Mar 25, 2009, 11:50:05 AM3/25/09
to Google Web Toolkit
Interesting question Ed.

I read this:

http://docstore.mik.ua/orelly/webprog/jscript/ch11_03.htm

and a couple of other things. Looks like different browsers do things
differently with javascript gc, with IE (surprise surprise) noticeably
differing from the others.

My feeling is that the JVM does this in Java basically and it's in the
Java spec that it does, so you can rely on it. I don't think there is
an equivalent javascipt spec for this behavior, so you probably cannot
rely on this working, x-browser anyway, and you probably have to
explicitly kill the listener yourself.

Ed

unread,
Mar 25, 2009, 12:03:04 PM3/25/09
to Google Web Toolkit
He Gregor,

I understand what you mean, but then still: in which way can this be
solved in a general and elegant way to "approach" the WeakReference
behavior in the JVM?

-- Ed
Reply all
Reply to author
Forward
0 new messages