Bug 563470 - [JFace] Widget is disposed on TreeViewer.getSelection
Summary: [JFace] Widget is disposed on TreeViewer.getSelection
Status: ASSIGNED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.14   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Christoph Laeubrich CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-05-22 05:59 EDT by Christoph Laeubrich CLA
Modified: 2020-05-25 05:20 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Christoph Laeubrich CLA 2020-05-22 05:59:33 EDT
If refresh (just as an example) is called but the widget is already disposed the following exception occurs.

It would be nice if TreeViewer#getSelection() would check if widget is disposed and if yes simply returns an empty selection

org.eclipse.swt.SWTException: Widget is disposed
	at org.eclipse.swt.SWT.error(SWT.java:4720)
	at org.eclipse.swt.SWT.error(SWT.java:4635)
	at org.eclipse.swt.SWT.error(SWT.java:4606)
	at org.eclipse.swt.widgets.Widget.error(Widget.java:550)
	at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:470)
	at org.eclipse.swt.widgets.Tree.getSelection(Tree.java:1923)
	at org.eclipse.jface.viewers.TreeViewer.getSelection(TreeViewer.java:231)
	at org.eclipse.jface.viewers.TreeViewer.setSelection(TreeViewer.java:308)
	at org.eclipse.jface.viewers.AbstractTreeViewer.setSelectionToWidget(AbstractTreeViewer.java:2561)
	at org.eclipse.jface.viewers.AbstractTreeViewer.setSelectionToWidget(AbstractTreeViewer.java:3016)
	at org.eclipse.jface.viewers.StructuredViewer.preservingSelection(StructuredViewer.java:1400)
	at org.eclipse.jface.viewers.TreeViewer.preservingSelection(TreeViewer.java:363)
	at org.eclipse.jface.viewers.StructuredViewer.preservingSelection(StructuredViewer.java:1354)
	at org.eclipse.jface.viewers.StructuredViewer.refresh(StructuredViewer.java:1454)
	at org.eclipse.jface.viewers.ColumnViewer.refresh(ColumnViewer.java:526)
	at org.eclipse.jface.viewers.StructuredViewer.refresh(StructuredViewer.java:1413)
Comment 1 Karsten Thoms CLA 2020-05-22 06:58:17 EDT
I'm not completely sure here what the right solution is. Also other methods in TreeViewer call the widget without check the Tree state. While it would be easy to fix this special and valid case, I'm wondering if other locations would be valid, too. And if, check them all or just with evidence that they cause issues like here?
Comment 2 Christoph Laeubrich CLA 2020-05-22 07:17:00 EDT
@Karsten Thoms it's a little bit a question of how "nice" JFace is supposed to react to disposal, it I'm currently adding checks "outside" its just a little bit cumbersome to always call:

>if(!treeViewer.getControl().isDisposed()) {
>	treeViewer.refresh();
>}

I personally would favor to guard the user against all kinds of underlying widget disposal, but maybe this is too much and better be solved step-by-step.

On the other hand one might argue that trying to use a viewer where the underlying widget is disposed is an error, its just a little bit odd to "decorate" dynamic doe all and always with all this "is EDT" "is not disposed" "is blablab" checks.

So maybe the check should only go into refresh() and variants?
Comment 3 Karsten Thoms CLA 2020-05-25 04:58:13 EDT
> So maybe the check should only go into refresh() and variants?

This would improve the situation a bit. Let's go for it. Would you provide a patch?
Comment 4 Andrey Loskutov CLA 2020-05-25 05:09:57 EDT
(In reply to Karsten Thoms from comment #3)
> > So maybe the check should only go into refresh() and variants?
> 
> This would improve the situation a bit. Let's go for it. Would you provide a
> patch?

I would vote -1 for that, because it will hide valid errors and lead to inconsistent behavior of the JFace API, that now will allow working with disposed widgets.

(In reply to Christoph Laeubrich from comment #2)
> On the other hand one might argue that trying to use a viewer where the
> underlying widget is disposed is an error

The code in question will definitely not do what it is supposed to do and still think that everything is "OK", while in fact the code will operate on a "dead" widget. Having errors helps a lot to find bugs. So I would close this bug as won't fix.
Comment 5 Christoph Laeubrich CLA 2020-05-25 05:20:57 EDT
Is it a bug do ignore a refresh of a no longer visble viewer?
From the API point of view, the Viewer API does not declare any exception to be thrown from that method.