Niclas,

I don't find your response conducive to a happy, open and learning
atmosphere.  If you have a suggestion, please make it, but colourful,
sarcastic language is not appreciated.

With regards to listeners - in a UI application a good portion of the
listeners added to objects are are going to be added for the duration
of the application's life.  Those listeners which are temporary should
be removed when they are no longer needed.

Typically the callback method on a listener will pass the instance of
the object being listened to it (maybe wrapped up in an Event object),
so the listener itself can clean up, e.g.

public void somethingHappened(Event event) {

   // ...

   event.source.removeListener(this);
}

Likewise, in the example being discussed, this can easily be achieved
in the following way:

// assuming that 'something' is local+final or is an instance variable
something.addListener(new Listener() {

   public void callback() {

       // ...

       something.removeListener(this);
   }

});

This is pretty standard stuff in my opinion and I don't think Pivot
can or should cater to the entire range of developer competency.

>From other UI SDKs:

- Android (which I've been using extensively recently)  'setListener'
instead of 'addListener' which at first I thought I would find
restrictive, but is actually quite liberating.

- Flex uses a functional programming approach for event handlers, one
of the arguments when setting an event handler function being whether
or not to use weak references for that handler.  My general view of
weak references is that they are not to be used lightly - it is very
hard to create deterministic application behaviour if you have things
being randomly garbage collected.  At least a memory leak will be
generally repeatable.

If anything weak references, when used incorrectly like you seem to be
proposing, encourage developers to be lazy and not care about proper
lifecycle management.  Would be much better for the whole
community/industry to educate developers that even with Java you have
to clean up after yourselves sometimes.

Regards,
Chris


2009/10/28 Niclas Hedhman <[email protected]>:
> On Wed, Oct 28, 2009 at 2:56 PM, Noel Grandin <[email protected]> wrote:
>> We had a discussion about this early on, but I think that was before we
>> moved to apache, so the archives are probably gone.
>>
>> The bottom line is that using weak references for listeners has nasty
>> corner cases.
>>
>> For example, if we do
>>
>>  something.addListener(new Listener() {
>>  });
>>
>> and we use a weak reference, the anonymous inner class will get GC'ed at
>> some random point in time, even if the parent class is still alive.
>
> My question is then; How do you expect to remove it??
>
> Isn't the above a stark reminder of shitty programming, where the
> programmer doesn't care about proper lifecycle managment so prevalent
> in today's programming shops??
>
> *sigh* I think I'll open a pub instead. *sigh*
>
> Cheers
> --
> Niclas Hedhman, Software Developer
> http://www.qi4j.org - New Energy for Java
>
> I  live here; http://tinyurl.com/2qq9er
> I  work here; http://tinyurl.com/2ymelc
> I relax here; http://tinyurl.com/2cgsug
>

Reply via email to