简体   繁体   English

Flex Spark Textinput防止GC收集组件

[英]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. 当从视图中删除该组件或切换视图时,我调用了我自己的dispose方法,该方法删除了剩余的eventListeners并杀死了一些引用,以便该组件最终可以被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. 一切正常,直到我将Spark TextInput添加到组件的MXML部分(花了我几个小时才能找出阻止组件收集的原因!),所以我确认TextInput会以某种方式自动添加一些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. 您可以查看SkinnableTextBase的代码,以了解内部附加了哪些事件侦听器。 Now that you know that, you can use hasEventListener() to test which ones weren't removed. 现在您已经知道了,可以使用hasEventListener()来测试哪些未被删除。 Using this technique we found that these listeners were still lingering: 使用这种技术,我们发现这些听众仍然挥之不去:

  • MouseEvent.MOUSE_DOWN MouseEvent.MOUSE_DOWN
  • TouchInteractionEvent.TOUCH_INTERACTION_START TouchInteractionEvent.TOUCH_INTERACTION_START

Removing them (preferably without subclassing TextInput) 删除它们 (最好不继承TextInput的子类)

Have a look at the code of SkinnableTextBase where these listeners are registered: 看一下注册了这些侦听器的SkinnableTextBase的代码:

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. 这意味着,如果将TextInput的interactionMode样式设置为InteractionMode.MOUSE ,则应删除侦听器。


Note: you might want to take a look at the JIRA bug base and file a bug if noone already has. 注意:您可能想看看JIRA错误库 ,如果还没有人,请提交一个错误。 Though I must say I'm not sure if this JIRA is still maintained now that Flex is moving to Apache. 尽管我必须说,由于Flex正在迁移到Apache,所以我不确定此JIRA是否仍然保持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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