use*_*478 6 javascript java mvp gwt memory-leaks
我按照此处给出的示例使用我的GWT应用程序中的MVP模式http://code.google.com/webtoolkit/doc/latest/tutorial/mvp-architecture.html
我为MainView中的每个面板都有一个MainPresenter和sub-presenter.为了展示一个新的子演示者,我做了这样的事情:
presenter = new PresenterA(new ViewA(), ....);
presenter.go(panel) // presenter clears the panel and itself to the panel
Run Code Online (Sandbox Code Playgroud)
何时PresenterA
创建,它将自己绑定到事件中ViewA
.我的问题是,切换到新演示者的正确方法是什么?现在,我只是创建一个新的演示者并将其附加到同一个面板,如下所示:
presenter = new PresenterB(new ViewB(), ....);
presenter.go(panel) // presenter clears the panel and itself to the panel
Run Code Online (Sandbox Code Playgroud)
我对这种方法有些怀疑.首先,当我切换到新的演示者时,我是否导致内存泄漏?我丢失了引用旧演示者的字段并将其从附加到的面板中清除.我想这意味着它应该是垃圾收集,但我不是很确定.其次,老主持人的事件绑定会发生什么?这些绑定是否会阻止演示者被垃圾收集?我需要先解开它们吗?
在没有内存泄漏和绑定到"死"事件的情况下处理切换演示者的情况的正确方法是什么.
我建议你看一下gwt-mvp和/或gwt-presenter库,它们都采用相同的方法解决这个问题.实际上,您可以为所有演示者创建一个基类,该基类维护演示者具有的所有事件注册的内部列表.当您再切换演示者时,可以调用presenter.unbind()
旧演示者,然后删除您创建的所有事件处理程序.
基本演示者类看起来像这样:
public abstract class BasePresenter {
private List<HandlerRegistration> registrations = Lists.newLinkedList();
public void bind() {}
public void unbind() {
for(HandlerRegistration registration : registrations) {
registration.removeHandler();
}
registrations.clear();
}
protected void addHandler(HandlerRegistration registration) {
registrations.add(registration);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在演示者的bind方法中,将HandlerRegistration
对象传递给addHandler()
方法:
bind() {
addHandler(foo.addBarHandler(...));
}
Run Code Online (Sandbox Code Playgroud)