Flex 3 Module的一个诡异bug

来源:互联网 发布:开淘宝店运费怎么算 编辑:程序博客网 时间:2024/04/27 21:44
表现:
使用ModuleManager去加载一个Module的时候, 所有的事件都激发不了.

代码:

  1.                 var testModule:IModuleInfo = ModuleManager.getModule('modules/pub/User.swf');
  2.                 testModule.addEventListener(ModuleEvent.READY,onModuleReady);
  3.                 testModule.addEventListener(ModuleEvent.SETUP, onModuleSetup);
  4.                 testModule.addEventListener(ModuleEvent.PROGRESS,onModuleLoading);
  5.                 testModule.load();

打个赌, ModuleEvent里所有的事件都不会被激发.


原因:

addEventListener之后, testModule的Event Listeners就可能被GC了.




解决:

不要声明局部IModuleInfo对象.


相关链接:
http://bugs.adobe.com/jira/browse/SDK-14021
https://bugs.adobe.com/jira/browse/SDK-11389

Gaurav Jain 同学说: 
"References to IModuleInfo must be maintained to keep the event listeneres alive. If the IModuleInfo is defined in function local scope the event listeners may get garbage collected. "
他表示:
"This is NAB. The weak reference was a change to fix a memory leak bug (https://bugs.adobe.com/jira/browse/SDK-9467) "

Not a bug~ 为了修复另一个bug, 牺牲这个....