Skip to content

Conversation

@amartya4256
Copy link
Contributor

@amartya4256 amartya4256 commented Jul 29, 2025

This PR releases the HandleCoreWebView2SwtCallback resource created by Edge browser in createControllerInitializationCallback after it has been registered by CreateCoreWebView2Controller method as otherwise the HandleCoreWebView2SwtCallback object can cause memory leaks if left unreleased.

fixes #2339

How to test:

  1. On master, test this snippet:
package org.eclipse.swt.snippets;

import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet138 {
	public static void main(String[] args) {
		Display display = new Display();
		Shell parentShell = new Shell(display);
		long allocatedMemory =  (Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory());
		long presumableFreeMemory = Runtime.getRuntime().maxMemory() - allocatedMemory;
		long blockSize = Math.min(Integer.MAX_VALUE / 2, presumableFreeMemory/10);
		int block = Math.toIntExact(blockSize);

		for(int i = 0; i < 100; i++) { // should not fail after 5 rounds

		    Shell shell = new Shell(parentShell);
		    Browser browser = new Browser(shell, SWT.BORDER);
		    shell.setLayout(new RowLayout());
		    browser.setUrl("http://example.com");
		    browser.setSize(500, 500);

		    shell.open();

		    browser.addTitleListener(new TitleListener() {

		        byte[] payload = new byte[block];

		        @Override
		        public void changed(TitleEvent event) {
		            System.out.println(payload[0]); // reference payload
		        }
		    });

		    shell.close();
		    browser.dispose();
		    browser = null;
		    shell=null;

		}
	}
}

It should throw an out of memory exception.
2. Run the snippet on this branch. The code should execute without any exception.

This commit releases the HandleCoreWebView2SwtCallback resource created by Edge browser in
createControllerInitializationCallback after it has been registered by
CreateCoreWebView2Controller method as otherwise the
HandleCoreWebView2SwtCallback object can cause memory leaks if left
unreleased.

fixes eclipse-platform#2339
@amartya4256 amartya4256 changed the title Release Edge callbacks after registering #2339 Release Edge callbacks after registering Jul 29, 2025
@amartya4256 amartya4256 linked an issue Jul 29, 2025 that may be closed by this pull request
@github-actions
Copy link
Contributor

Test Results

   546 files  ±0     546 suites  ±0   29m 49s ⏱️ + 2m 7s
 4 412 tests ±0   4 395 ✅ ±0   17 💤 ±0  0 ❌ ±0 
16 718 runs  ±0  16 591 ✅ ±0  127 💤 ±0  0 ❌ ±0 

Results for commit 8b36722. ± Comparison against base commit bcc58ad.

Copy link
Member

@fedejeanne fedejeanne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you @amartya4256 !

I changed the snippet a little since it didn't work on my machine (there was an ArithmethicException when calling Math::toIntExact)

@fedejeanne fedejeanne merged commit fc94eaf into eclipse-platform:master Jul 29, 2025
17 checks passed
@fedejeanne fedejeanne deleted the amartya4256/edge_memory_leak branch July 29, 2025 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Edge] Browser object leaked memory [Edge] Browser object leaked memory

2 participants