Skip to content

Commit 4cb81af

Browse files
ptzieglerlaeubi
authored andcommitted
Remove preference listener if manager is shut down or project is deleted
The PreferenceChangeListener that is added when a project is created/opened or when the plugin is started needs to be removed when the project is deleted, in order to make it eligible for garbage collection.
1 parent 917d9a0 commit 4cb81af

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/WorkspaceModelManager.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2003, 2022 IBM Corporation and others.
2+
* Copyright (c) 2003, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -152,7 +152,11 @@ protected void addListeners() {
152152

153153
@Override
154154
protected void removeListeners() {
155-
PDECore.getWorkspace().removeResourceChangeListener(this);
155+
IWorkspace workspace = PDECore.getWorkspace();
156+
if (bundleRootChangedListener != null) {
157+
Arrays.stream(workspace.getRoot().getProjects()).forEach(this::removeBundleRootChangedListener);
158+
}
159+
workspace.removeResourceChangeListener(this);
156160
super.removeListeners();
157161
}
158162

@@ -198,6 +202,9 @@ public boolean visit(IResourceDelta delta) throws CoreException {
198202
if (addedOrOpened && bundleRootChangedListener != null) {
199203
addBundleRootChangedListener(project);
200204
}
205+
if (delta.getKind() == IResourceDelta.REMOVED && bundleRootChangedListener != null) {
206+
removeBundleRootChangedListener(project);
207+
}
201208
if (isInterestingProject(project) && addedOrOpened) {
202209
createModel(project, true);
203210
return false;
@@ -227,6 +234,13 @@ private void addBundleRootChangedListener(IProject project) {
227234
pdeNode.addPreferenceChangeListener(bundleRootChangedListener);
228235
}
229236

237+
private void removeBundleRootChangedListener(IProject project) {
238+
IEclipsePreferences pdeNode = new ProjectScope(project).getNode(PDECore.PLUGIN_ID);
239+
// Remove the preference change listener when the project is removed
240+
// from the workspace to make it eligible for garbage collection
241+
pdeNode.removePreferenceChangeListener(bundleRootChangedListener);
242+
}
243+
230244
protected IPreferenceChangeListener createBundleRootChangeListener() {
231245
return e -> {
232246
if (PDEProject.BUNDLE_ROOT_PATH.equals(e.getKey()) && !isInRemovedBranch(e.getNode())) {

0 commit comments

Comments
 (0)