If the event never happens again, you're probably wasting time cleaning
up.  addEventListener essentially pushes the listener onto an internal
array.  If you remove it, it isn't clear the array will optimize to give
up the memory.

 

I hadn't thought about using arguments.callee.  I'd verify it works on
the release player, but like I said, it just may not be worth the
effort.  The array slot was probably 4 bytes and the code to remove is
going to be more than that.

 

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of dbronk
Sent: Sunday, August 10, 2008 7:15 AM
To: [email protected]
Subject: [flexcoders] Re: Should I remove listeners declared in mxml?
ie: Best Practice

 

Thanks Alex. This is what I had thought for as long as I've been
using flex. But we were discussing two things in our office:

1. I realize that using an event attribute in mxml does not cause a
leak, but we were thinking that it would help with
performance/overhead/etc. if we still remove any listener after we
don't need it anymore. This includes listeners from using an event
attribute in mxml as well as calling addEventListener. Is this true?

2. A colleague of mine found this little gem of a statement for being
able to remove the listener. 
event.target.removeEventListener(event.type, arguments.callee); I
hadn't seen this before. The only warning I could come up with was
that if the event was bubbled then event.target may not be what we
want. What do you think?

Thanks again and I'm very curious to see your replies as I'm sure this
will be a large debate in our office.

Dale

--- In [email protected] <mailto:flexcoders%40yahoogroups.com>
, "Alex Harui" <[EMAIL PROTECTED]> wrote:
>
> OTOH, there is no situation where you need to call removeEventListener
> for any event attribute in MXML. The references created do not cause
> memory leaks.
> 
> 
> 
> Furthermore, you can't call removeEventListener(...,
onCreationComplete)
> bececause what got called behind the scenes was
> 
> 
> 
> addEventListener(..., someWrapperFunctionWithAGeneratedName);
> 
> function someWrapperFunctionWithAGeneratedName(event:Event):void
> 
> {
> 
> onCreationComplete()
> 
> }
> 
> 
> 
> And you don't know the name of that function.
> 
> 
> 
> ________________________________
> 
> From: [email protected] <mailto:flexcoders%40yahoogroups.com>
[mailto:[email protected] <mailto:flexcoders%40yahoogroups.com>
] On
> Behalf Of dbronk
> Sent: Saturday, August 09, 2008 9:28 AM
> To: [email protected] <mailto:flexcoders%40yahoogroups.com> 
> Subject: [flexcoders] Re: Should I remove listeners declared in mxml?
> ie: Best Practice
> 
> 
> 
> One extra point. I realize that if I use the
> addEventListener("blahEvent", myFunc) function I need to remove those.
> Even if I use weak reference I should remove it explicitly when I'm
> done with the listener rather than waiting for GC.
> 
> Dale
> 
> --- In [email protected]
<mailto:flexcoders%40yahoogroups.com>
<mailto:flexcoders%40yahoogroups.com>
> , "dbronk" <dbronk@> wrote:
> >
> > Subject is a bit confusing.
> > 
> > What I'm asking is, for example, let's say that I have a component
> > where I code creationComplete="onCreationComplete()". Should I make
> > sure that in the onCreationComplete function do a
> > this.removeListener(FlexEvent.CREATION_COMPLETE,
onCreationComplete)?
> > 
> > I guess my real question is that if I don't, does that leave an open
> > listener lying around that can never be called again?
> > 
> > Same question for custom events. If I have a component that
> > dispatches "someEvent" and I have mxml that does
> > 
> > <comps:MyComp someEvent="onSomeEvent(event)" />
> > 
> > And the design of this is that someEvent will ever be dispatched a
> > single time in it's existence should I call removeEventListener in
my
> > onSomeEvent function?
> > 
> > I have written the following in my onSomeEvent function:
> > 
> > var hasEvent : Boolean = this.hasEventListener("someEvent");
> > 
> > this.removeEventListener("someEvent", onSomeEvent);
> > 
> > hasEvent = this.hasEventListener("someEvent");
> > 
> > 
> > The results of this is that hasEvent is true both before and after I
> > call the removeEventListener.
> > 
> > Along with this, is there a way to see if there is a specific
> > listener? I mean currently I am only checking if there is a listener
> > on that event, not that event and that function.
> > 
> > Thanks,
> > Dale
> >
>

 

Reply via email to