Skip to content

Commit 483d87d

Browse files
basilevsiloveeclipse
authored andcommitted
Fix handler leak #925
Fixes #925 Handlers can store references to external objects so cleaning them up when another handler is activated is not quick enough. WeakReference is enough to detect handler switch and a cleanup of collected IObjectWithState is (hopefully) not necessary.
1 parent 5b62380 commit 483d87d

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

bundles/org.eclipse.e4.core.commands/src/org/eclipse/e4/core/commands/internal/HandlerServiceHandler.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package org.eclipse.e4.core.commands.internal;
1616

17+
import java.lang.ref.WeakReference;
1718
import org.eclipse.core.commands.AbstractHandlerWithState;
1819
import org.eclipse.core.commands.ExecutionEvent;
1920
import org.eclipse.core.commands.ExecutionException;
@@ -43,7 +44,7 @@ public class HandlerServiceHandler extends AbstractHandlerWithState {
4344

4445
protected final String commandId;
4546
// Remove state from currentStateHandler when it goes out of scope
46-
private IObjectWithState currentStateHandler;
47+
private WeakReference<IObjectWithState> currentStateHandler = new WeakReference<IObjectWithState>(null);
4748

4849
public HandlerServiceHandler(String commandId) {
4950
this.commandId = commandId;
@@ -250,11 +251,12 @@ private void switchHandler(Object handler) {
250251
} else {
251252
typed = null;
252253
}
253-
IObjectWithState oldHandler = currentStateHandler;
254+
255+
IObjectWithState oldHandler = currentStateHandler.get();
254256
if (oldHandler == typed) {
255257
return;
256258
}
257-
currentStateHandler = typed;
259+
currentStateHandler = new WeakReference<>(typed);
258260
for (String id : getStateIds()) {
259261
if (oldHandler != null) {
260262
oldHandler.removeState(id);

0 commit comments

Comments
 (0)