-
Notifications
You must be signed in to change notification settings - Fork 210
Upstream memory leak fix for ScopedPreferenceStore #2878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ | |
* IBM Corporation - initial API and implementation | ||
* Yves YANG <[email protected]> - | ||
* Initial Fix for Bug 138078 [Preferences] Preferences Store for i18n support | ||
* Patrick Aigner - | ||
* upstream fix created by Sebastian Zarnekow <[email protected]> | ||
* Fix for Bug 239033 [Preferences] ScopedPreferenceStore causes memory leak | ||
*******************************************************************************/ | ||
package org.eclipse.ui.preferences; | ||
|
||
|
@@ -24,11 +27,9 @@ | |
import org.eclipse.core.runtime.SafeRunner; | ||
import org.eclipse.core.runtime.preferences.DefaultScope; | ||
import org.eclipse.core.runtime.preferences.IEclipsePreferences; | ||
import org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener; | ||
import org.eclipse.core.runtime.preferences.IEclipsePreferences.NodeChangeEvent; | ||
import org.eclipse.core.runtime.preferences.IScopeContext; | ||
import org.eclipse.jface.preference.IPersistentPreferenceStore; | ||
import org.eclipse.jface.preference.IPreferenceStore; | ||
import org.eclipse.jface.resource.JFaceResources; | ||
import org.eclipse.jface.util.IPropertyChangeListener; | ||
import org.eclipse.jface.util.PropertyChangeEvent; | ||
|
@@ -51,7 +52,7 @@ | |
* @see org.eclipse.core.runtime.preferences | ||
* @since 3.1 | ||
*/ | ||
public class ScopedPreferenceStore extends EventManager implements IPreferenceStore, IPersistentPreferenceStore { | ||
public class ScopedPreferenceStore extends EventManager implements IPersistentPreferenceStore { | ||
|
||
/** | ||
* The storeContext is the context where values will stored with the setValue | ||
|
@@ -77,6 +78,12 @@ public class ScopedPreferenceStore extends EventManager implements IPreferenceSt | |
*/ | ||
IEclipsePreferences.IPreferenceChangeListener preferencesListener; | ||
|
||
/** | ||
* The listener on the IEclipsePreferences that the {@link #preferencesListener} | ||
* is registered to new preference nodes in the underlying store. | ||
*/ | ||
private IEclipsePreferences.INodeChangeListener nodeChangeListener; | ||
|
||
/** | ||
* The default context is the context where getDefault and setDefault methods | ||
* will search. This context is also used in the search. | ||
|
@@ -123,30 +130,46 @@ public ScopedPreferenceStore(IScopeContext context, String qualifier) { | |
storeContext = context; | ||
this.nodeQualifier = qualifier; | ||
this.defaultQualifier = qualifier; | ||
|
||
((IEclipsePreferences) getStorePreferences().parent()).addNodeChangeListener(getNodeChangeListener()); | ||
} | ||
|
||
// Fix is here and in disposeNodeChangeListener. Look for callees accordingly. | ||
/** | ||
* Return a node change listener that adds a removes the receiver when nodes | ||
* change. | ||
* | ||
* @return INodeChangeListener | ||
* Initialize the node change listener. | ||
*/ | ||
private INodeChangeListener getNodeChangeListener() { | ||
return new IEclipsePreferences.INodeChangeListener() { | ||
@Override | ||
public void added(NodeChangeEvent event) { | ||
if (nodeQualifier.equals(event.getChild().name()) && isListenerAttached()) { | ||
getStorePreferences().addPreferenceChangeListener(preferencesListener); | ||
private void initializeNodeChangeListener() { | ||
if (nodeChangeListener == null) { | ||
nodeChangeListener = new IEclipsePreferences.INodeChangeListener() { | ||
@Override | ||
public void added(NodeChangeEvent event) { | ||
if (nodeQualifier.equals(event.getChild().name()) && isListenerAttached()) { | ||
getStorePreferences().addPreferenceChangeListener(preferencesListener); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void removed(NodeChangeEvent event) { | ||
// Do nothing as there are no events from removed node | ||
@Override | ||
public void removed(NodeChangeEvent event) { | ||
// Do nothing as there are no events from removed node | ||
} | ||
}; | ||
((IEclipsePreferences) getStorePreferences().parent()).addNodeChangeListener(nodeChangeListener); | ||
} | ||
} | ||
|
||
/** | ||
* Dispose the node change listener. | ||
*/ | ||
private void disposeNodeChangeListener() { | ||
if (nodeChangeListener != null) { | ||
IEclipsePreferences preferences = getStorePreferences(); | ||
if (preferences == null) { | ||
return; | ||
} | ||
}; | ||
IEclipsePreferences parent = (IEclipsePreferences) preferences.parent(); | ||
if (parent == null) | ||
return; | ||
parent.removeNodeChangeListener(nodeChangeListener); | ||
nodeChangeListener = null; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -224,8 +247,9 @@ private IEclipsePreferences getDefaultPreferences() { | |
|
||
@Override | ||
public void addPropertyChangeListener(IPropertyChangeListener listener) { | ||
initializePreferencesListener();// Create the preferences listener if it | ||
// does not exist | ||
// Create the preference listeners if they do not exist | ||
initializeNodeChangeListener(); | ||
initializePreferencesListener(); | ||
addListenerObject(listener); | ||
} | ||
|
||
|
@@ -465,6 +489,7 @@ public void removePropertyChangeListener(IPropertyChangeListener listener) { | |
removeListenerObject(listener); | ||
if (!isListenerAttached()) { | ||
disposePreferenceStoreListener(); | ||
disposeNodeChangeListener(); | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like the life-cycle of the listener is solely bound to the
IPropertyChangeListener
, I therfore think it would be better to instead of handling it "globally" to instead have a wrapped listener object that is forwarding the events directly instead of holding an own instance in the store.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean with a wrapped listener object?
It isn't dependent on a single
IPropertyChangeListener
but on the fact if any listeners are attached at all - no use listening if there is nothing to forward it to.This copies over the relevant changes from: https://github.com/eclipse-xtext/xtext/blob/main/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/preferences/FixedScopedPreferenceStore.java
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of one "global" listener used for all
IPropertyChangeListener
it should wrap this one into the required type and use this for the life-cycle management.