简体   繁体   中英

Flex Spark Textinput prevents component to be collected by GC

I've got a custom component (quite complex so I can't post any code here, although that shouldn't matter), that I can add to a view. When the component is deleted from the view or the view is switched I call my own dispose method which removes remaining eventListeners and kills some references so that the component can eventually be nulled and collected by the GC.

All that works perfectly fine until I add a Spark TextInput to the MXML part of the component (it took me hours to find out what is preventing the component to be collected!), so I recon that the TextInput somehow automatically adds some eventListeners.

My question is what are these listeners, or is there anything else I haven't thought of?

Any help would be greatly appreciated!

I'll summarize our discussion for the pleasure of future readers.

Find the culprit

You could can have a look at the code of SkinnableTextBase to see what event listeners are attached internally. Now that you know that, you can use hasEventListener() to test which ones weren't removed. Using this technique we found that these listeners were still lingering:

  • MouseEvent.MOUSE_DOWN
  • TouchInteractionEvent.TOUCH_INTERACTION_START

Removing them (preferably without subclassing TextInput)

Have a look at the code of SkinnableTextBase where these listeners are registered:

override public function styleChanged(styleProp:String):void
{
    super.styleChanged(styleProp);

    if (!styleProp ||
        styleProp == "styleName" || styleProp == "interactionMode")
    {
        if (getStyle("interactionMode") == InteractionMode.TOUCH && !touchHandlersAdded)
        {
            addEventListener(MouseEvent.MOUSE_DOWN, touchMouseDownHandler);
            addEventListener(TouchInteractionEvent.TOUCH_INTERACTION_START,
                touchInteractionStartHandler);
            touchHandlersAdded = true;
        }
        else if (getStyle("interactionMode") == InteractionMode.MOUSE && touchHandlersAdded)
        {
            removeEventListener(MouseEvent.MOUSE_DOWN, touchMouseDownHandler);
            removeEventListener(TouchInteractionEvent.TOUCH_INTERACTION_START,
                touchInteractionStartHandler);
            touchHandlersAdded = false;
        }
    }
}

This means that if you set the TextInput's interactionMode style to InteractionMode.MOUSE , that should remove the listeners.


Note: you might want to take a look at the JIRA bug base and file a bug if noone already has. Though I must say I'm not sure if this JIRA is still maintained now that Flex is moving to Apache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM