Bug 510944 - Fix memory leak in EclipseContext

In multiuser environment (RAP) EclipseContext#notifyOnDisposal set keeps
a reference to already disposed display object (through UISynchronize)
after the context is disposed.

Clear EclipseContext#notifyOnDisposal set after the listeners are
notified in dispose().

510944: Memory leak in EclipseContext
https://bugs.eclipse.org/bugs/show_bug.cgi?id=510944

Change-Id: Ia4d1f6b116a9df35f57a07d07c82c0ced040dba1
Signed-off-by: Ivan Furnadjiev <ivan@eclipsesource.com>
(cherry picked from commit bc866e66a5a397bb1363843e4106fd47a6009aef)
diff --git a/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/EclipseContext.java b/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/EclipseContext.java
index 442bfa2..009fec9 100644
--- a/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/EclipseContext.java
+++ b/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/internal/contexts/EclipseContext.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009, 2016 IBM Corporation and others.
+ * Copyright (c) 2009, 2017 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -181,6 +181,7 @@
 			for (IContextDisposalListener listener : notifyOnDisposal) {
 				listener.disposed(this);
 			}
+			notifyOnDisposal.clear();
 		}
 
 		for (ValueComputation computation : localValueComputations.values()) {
diff --git a/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/contexts/EclipseContextTest.java b/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/contexts/EclipseContextTest.java
index 238d1b4..d02a697 100644
--- a/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/contexts/EclipseContextTest.java
+++ b/tests/org.eclipse.e4.core.tests/src/org/eclipse/e4/core/internal/tests/contexts/EclipseContextTest.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009, 2015 IBM Corporation and others.
+ * Copyright (c) 2009, 2017 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -22,6 +22,7 @@
 import org.eclipse.e4.core.contexts.RunAndTrack;
 import org.eclipse.e4.core.di.IInjector;
 import org.eclipse.e4.core.internal.contexts.EclipseContext;
+import org.eclipse.e4.core.internal.contexts.IContextDisposalListener;
 import org.junit.Before;
 import org.junit.Test;
 import org.osgi.framework.FrameworkUtil;
@@ -108,6 +109,20 @@
 		assertTrue(((EclipseContext)parentContext).getChildren().isEmpty());
 	}
 
+	@Test
+	public void testDisposeClearsNotifyOnDisposalSet() {
+		((EclipseContext) context).notifyOnDisposal(new IContextDisposalListener() {
+			@Override
+			public void disposed(IEclipseContext context) {
+				runCounter++;
+			}
+		});
+		context.dispose();
+		assertEquals(1, runCounter);
+		context.dispose();
+		assertEquals(1, runCounter);
+	}
+
 	/**
 	 * Tests handling of a context function defined in the parent that uses values defined in the
 	 * child