Skip to content

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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down Expand Up @@ -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
Copy link
Contributor

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.

Copy link
Author

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

Copy link
Contributor

Choose a reason for hiding this comment

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

It isn't dependent on a single IPropertyChangeListener but on the fact if any listeners are attached at all

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.

initializeNodeChangeListener();
initializePreferencesListener();
addListenerObject(listener);
}

Expand Down Expand Up @@ -465,6 +489,7 @@ public void removePropertyChangeListener(IPropertyChangeListener listener) {
removeListenerObject(listener);
if (!isListenerAttached()) {
disposePreferenceStoreListener();
disposeNodeChangeListener();
}
}

Expand Down