Changeset 18321 in josm for trunk/src


Ignore:
Timestamp:
2021-11-09T13:42:26+01:00 (3 years ago)
Author:
GerdP
Message:

fix #21446: Memory leaks with TaggingPreset

  • don't always add ActiveLayerChangeListener in TaggingPreset constructor, only needed for some instances
  • let ToolbarPreferences implement TaggingPresetListener to update toolbar and free (most) refs to presets when a preset was uninstalled
Location:
trunk/src/org/openstreetmap/josm/gui
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/ToolbarPreferences.java

    r18208 r18321  
    7777import org.openstreetmap.josm.gui.help.HelpUtil;
    7878import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
     79import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetListener;
     80import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
    7981import org.openstreetmap.josm.gui.util.GuiHelper;
    8082import org.openstreetmap.josm.gui.util.ReorderableTableModel;
     
    9193 * @since 172
    9294 */
    93 public class ToolbarPreferences implements PreferenceSettingFactory {
     95public class ToolbarPreferences implements PreferenceSettingFactory, TaggingPresetListener {
    9496
    9597    private static final String EMPTY_TOOLBAR_MARKER = "<!-empty-!>";
     
    10231025        });
    10241026        MapFrame.TOOLBAR_VISIBLE.addListener(e -> refreshToolbarControl());
     1027        TaggingPresets.addListener(this);
    10251028    }
    10261029
     
    11391142        }
    11401143        if (toolbar != null) {
     1144            actions.put(toolbar, action);
    11411145            regactions.put(toolbar, action);
    11421146        }
     
    11531157        Object toolbar = action.getValue("toolbar");
    11541158        if (toolbar instanceof String) {
     1159            actions.remove(toolbar);
    11551160            return regactions.remove(toolbar);
    11561161        }
     
    12851290
    12861291    private static final DataFlavor ACTION_FLAVOR = new DataFlavor(ActionDefinition.class, "ActionItem");
     1292
     1293    @Override
     1294    public void taggingPresetsModified() {
     1295        refreshToolbarControl();
     1296    }
    12871297}
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPreset.java

    r18221 r18321  
    178178     */
    179179    public TaggingPreset() {
    180         MainApplication.getLayerManager().addActiveLayerChangeListener(this);
    181180        updateEnabledState();
    182181    }
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresets.java

    r18281 r18321  
    9191            if (!(tp instanceof TaggingPresetSeparator)) {
    9292                MainApplication.getToolbar().register(tp);
     93                MainApplication.getLayerManager().addActiveLayerChangeListener(tp);
    9394            }
    9495        }
     
    138139    public static void destroy() {
    139140        ToolbarPreferences toolBar = MainApplication.getToolbar();
    140         taggingPresets.forEach(toolBar::unregister);
     141        for (TaggingPreset tp: taggingPresets) {
     142            toolBar.unregister(tp);
     143            if (!(tp instanceof TaggingPresetSeparator)) {
     144                MainApplication.getLayerManager().removeActiveLayerChangeListener(tp);
     145            }
     146        }
    141147        taggingPresets.clear();
    142148        PRESET_TAG_CACHE.clear();
    143149        PRESET_ROLE_CACHE.clear();
    144150        MainApplication.getMenu().presetsMenu.removeAll();
    145         listeners.forEach(TaggingPresetListener::taggingPresetsModified);
    146151    }
    147152
Note: See TracChangeset for help on using the changeset viewer.