Bug 477086 - [Browser] org.eclipse.swt.browser.Browser doesn't dispose itself in case of SWTErrors
Summary: [Browser] org.eclipse.swt.browser.Browser doesn't dispose itself in case of S...
Status: CLOSED WORKSFORME
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 4.4.2   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Platform-SWT-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: triaged
Depends on:
Blocks:
 
Reported: 2015-09-10 12:13 EDT by Martin Niedermair CLA
Modified: 2023-08-28 04:43 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Niedermair CLA 2015-09-10 12:13:32 EDT
If a problem occurs when instantiating a browser, like e.g. SWTError triggered by GTK3 and mozilla, the Browser widget doesn't dispose itself:

In the constructor for org.eclipse.swt.browser.Browser as shown below,
and exception may be thrown in webBrowser.create (parent, style); 
If that happens, dispose() won't be called, but the corresponding Control was already added to the parent composite.


public Browser (Composite parent, int style) {
	super (checkParent (parent), checkStyle (style));
	userStyle = style;

	String platform = SWT.getPlatform ();
	if ("gtk".equals (platform)) { //$NON-NLS-1$
		parent.getDisplay ().setData (NO_INPUT_METHOD, null);
	}

	style = getStyle ();
	webBrowser = new BrowserFactory ().createWebBrowser (style);
	if (webBrowser != null) {
		webBrowser.setBrowser (this);
		webBrowser.create (parent, style); /** may throw SWTError */
		return;
	}
	dispose ();
	SWT.error (SWT.ERROR_NO_HANDLES);
}
Comment 1 bongsuk Ko CLA 2015-09-17 00:30:54 EDT
Can you give a code which reproduce this bug?
Comment 2 Martin Niedermair CLA 2015-09-17 03:24:10 EDT
Sure, please find below - let me know if this helps.

Basically what I'm trying to do doing is:

void Control addTextControl(Composite parent) {
	Control	control = null;
	try {
		control = new Browser(parent, SWT.BORDER);
		fillBrowserControlWithContent(control);
	} catch (SWTError e) {
		/** In the case I'm reported, the Browser's constructor has thrown an SWTError triggered by GTK3 and mozilla:
		 * " [Browser style SWT.MOZILLA and Java system property org.eclipse.swt.browser.DefaultType=mozilla are not 
		 * supported with GTK 3 as XULRunner is not ported for GTK 3 yet]"
		 *
		 * control will stil be null, but the Browser's widget wasn't disposed, so it is still a child of 'parent'.
		 */
		/** Browser didn't work out: Use the old technique */
		control = new ScrolledFormText(comp, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.WRAP, true);
		fillFormTextControlWithContent(control);
	}
Comment 3 Martin Niedermair CLA 2015-09-17 03:51:18 EDT
Please ignore the 'void' in my previous sample - I really should get a coffee before posting sample code - sorry for that.
Comment 4 Alexander Kurtakov CLA 2017-12-06 05:26:29 EST
What is the state of this bug? Mozilla support has been removed so maybe we should close this one?
Comment 5 Leo Ufimtsev CLA 2018-04-10 15:14:20 EDT
It would be useful to wrap this around in a try/catch block.
Comment 6 Eric Williams CLA 2019-02-01 14:16:39 EST
No comment from the reporter in awhile so I am going to close this. Please open a new ticket if you face this issue using SWT 4.10 with WebKit2.
Comment 7 richa agrawal CLA 2023-08-28 01:46:39 EDT
I am on the SWT3.121.0 version. 
When trying to create a browser instance for Edge and not having an edge on that system I am getting a SWT error and to handle that I am catching that error and trying to create another instance using the IE browser.
But the Edge instance is not getting disposed of itself so I disposed of it via code but when Calling Browser.clearSessions() after creating the IE browser instance it is still using the Edge instance and going inside Edge class and throwing SWT error.
How to dispose of the previous Browser instance?
Code for the same -
public static Browser getBrowserInstance(Composite parent, int style) {
    try {
      browser = new Browser(parent, style);\
    } catch (SWTError error) {
      for (Control children : parent.getChildren()) {
        if (children instanceof Browser) {
          children.dispose();
          parent.layout(true, true);
        }
      }
      if (error.code == SWT.ERROR_NOT_IMPLEMENTED) {
        browser = new Browser(parent, SWT.NONE);

      }
    }
    return browser;
  }
Comment 8 Alexander Kurtakov CLA 2023-08-28 04:43:24 EDT
SWT development moved to https://github.com/eclipse-platform/eclipse.platform.swt . So if you can reproduce with latest version of SWT and ideally come up with snippet to demonstrate the issue please report there.