调试“Widget is disposed”异常

Debugging a `Widget is disposed` exception

我们的应用程序中抛出一个小部件异常,我正在努力寻找导致问题的小部件。

它是一个具有多个视角和视图的 RCP 4 应用程序。当异常发生时,堆栈跟踪显示最后一段被调用的代码,但没有告诉我们哪个小部件已被处理掉。

我试过在 Widgetdispose 方法上设置断点,但是要处理的小部件太多了,这是禁止的。

!ENTRY org.eclipse.e4.ui.workbench 4 0 2019-04-26 10:14:00.114
!MESSAGE Internal Error
!STACK 0
org.eclipse.swt.SWTException: Widget is disposed
    at org.eclipse.swt.SWT.error(SWT.java:4595)
    at org.eclipse.swt.SWT.error(SWT.java:4510)
    at org.eclipse.swt.SWT.error(SWT.java:4481)
    at org.eclipse.swt.widgets.Widget.error(Widget.java:451)
    at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:350)
    at org.eclipse.swt.widgets.Control.computeSize(Control.java:663)
    at org.eclipse.swt.custom.ScrolledCompositeLayout.computeSize(ScrolledCompositeLayout.java:36)
    at org.eclipse.swt.widgets.Composite.computeSizeInPixels(Composite.java:218)
    at org.eclipse.swt.widgets.Control.computeSize(Control.java:666)
    at org.eclipse.swt.layout.GridData.computeSize(GridData.java:494)
    at org.eclipse.swt.layout.GridLayout.layout(GridLayout.java:224)
    at org.eclipse.swt.layout.GridLayout.layout(GridLayout.java:200)
    at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1362)
    at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1369)
    at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1369)
    at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1369)
    at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1369)
    at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1369)
    at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1369)
    at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1369)
    at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1369)
    at org.eclipse.swt.widgets.Composite.updateLayout(Composite.java:1347)
    at org.eclipse.swt.widgets.Composite.setLayoutDeferred(Composite.java:1148)
    at org.eclipse.swt.widgets.Display.runDeferredLayouts(Display.java:3968)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3540)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1173)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:339)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1062)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:156)
    at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:166)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:137)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:107)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:400)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:659)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:595)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1501)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1474)

查找小部件被处置异常的原因的最佳方法是什么?

这原来与@greg-449 提到的 ScrolledComposite 有关。我第一次看的时候错过了。

我们使用父组合来创建 ScrolledComposite 然后设置内容。正在处理内容,但仍存储引用,导致 widget is disposed 异常。

final ScrolledComposite scroll = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.BORDER);
scroll.setContent(widgetThatIsDisposedLater);

为了解决这个问题,我处理了 ScrolledComposite 这意味着它将不再尝试使用其处理的内容。