在代理中我应该显式删除侦听器吗?
目前我正在开发 Flex 应用程序,其中使用 puremvc 的多核变体。我的问题是在我的代理中,我正在进行远程调用并附加一些(结果和故障)事件侦听器。那么在我的事件处理程序代码中,我是否应该显式删除侦听器以使 RemoteObject 类符合垃圾收集条件?
public function getTableGridData():void
{
var hostController:RemoteObject=this.hostController("ABC");
hostController.addEventListener(ResultEvent.RESULT, handleResult);
hostController.addEventListener(FaultEvent.FAULT, handleFault);
hostController.getTableData();
}
private function handleResult(event:ResultEvent):void
{
ApplicationFacade.getInstance(key).sendNotification("abc", event.result);
}
所以这里的 hostController 持有两个监听器的强引用。因此,在 resultEvent 之后,hostController 是否有资格进行垃圾收集,或者我必须提到侦听器的弱引用,以使 hostController 有资格进行垃圾收集?
Currently I am working on flex application where I am using multicore variant of puremvc. My question is in my proxy I am making remote call and attaching some (RESULT and FAULT) event listener. So in my event handler code should I remove listeners explicitly for making remoteObject class eligible for garbage collecton ?
public function getTableGridData():void
{
var hostController:RemoteObject=this.hostController("ABC");
hostController.addEventListener(ResultEvent.RESULT, handleResult);
hostController.addEventListener(FaultEvent.FAULT, handleFault);
hostController.getTableData();
}
private function handleResult(event:ResultEvent):void
{
ApplicationFacade.getInstance(key).sendNotification("abc", event.result);
}
So here hostController holds strong reference of both the listeners. So after resultEvent does hostController is eligible for garbage collection or I have to mention weak reference for listeners for making hostController eligible for garbage collectioin ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你应该明确删除听众。
至少可以让大家更容易阅读代码。
我不确定您是否保留对该 hostController 的任何其他引用(正如您从 hostController() 获得的那样)。
如果您没有任何其他引用(例如,如果 hostController() 是一个简单的 create-forget 工厂)并在这些侦听器上使用弱引用,则意味着它甚至可以在之前进行垃圾回收据我了解,它完成了它的工作。
I think you should remove the listeners explicitly.
It would at least make it easier for everyone to read the code.
I'm not sure if you keep any other references to that hostController (as you got it from hostController()).
If you don't have any other references (for example, if hostController() is a simple create-forget factory) and use weak references on those listeners, that would mean it's eligible for garbage collection even before it finishes it's work -- as far as I understand it.