Ignore:
Timestamp:
2024-03-03T10:25:25+01:00 (22 months ago)
Author:
GerdP
Message:

fix #23521: fix some memory leaks

  • dispose dialogs
  • either avoid to create clones of ways or relations or use setNodes(null) / setMembers(null)
  • replaces most ChangeCommand instances by better specialized alternatives
  • add some comments
  • fix some checkstyle / sonar issues
File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/reltoolbox/src/relcontext/actions/TheRing.java

    r36134 r36217  
    1515import org.openstreetmap.josm.command.AddCommand;
    1616import org.openstreetmap.josm.command.ChangeCommand;
     17import org.openstreetmap.josm.command.ChangeMembersCommand;
     18import org.openstreetmap.josm.command.ChangeNodesCommand;
     19import org.openstreetmap.josm.command.ChangePropertyCommand;
    1720import org.openstreetmap.josm.command.Command;
    1821import org.openstreetmap.josm.command.DeleteCommand;
     
    330333    public List<Command> getCommands(boolean createMultipolygon, Map<Relation, Relation> relationChangeMap) {
    331334        Way sourceCopy = new Way(source);
     335        Map<String, String> tagsToRemove = new HashMap<>();
    332336        if (createMultipolygon) {
    333337            Collection<String> linearTags = Config.getPref().getList(PREF_MULTIPOLY + "lineartags",
     
    335339            relation = new Relation();
    336340            relation.put("type", "multipolygon");
    337             for (String key : sourceCopy.keySet()) {
    338                 if (linearTags.contains(key)) {
     341            for (String key : source.keySet()) {
     342                if (linearTags.contains(key)
     343                        || ("natural".equals(key) && "coastline".equals(source.get("natural"))))
    339344                    continue;
    340                 }
    341                 if ("natural".equals(key) && "coastline".equals(sourceCopy.get("natural"))) {
    342                     continue;
    343                 }
    344                 relation.put(key, sourceCopy.get(key));
     345               
     346                relation.put(key, source.get(key));
    345347                sourceCopy.remove(key);
     348                tagsToRemove.put(key, null);
    346349            }
    347350        }
     
    354357                Relation rel;
    355358                if (relationChangeMap != null) {
    356                     if (relationChangeMap.containsKey(p)) {
    357                         rel = relationChangeMap.get(p);
    358                     } else {
     359                    rel = relationChangeMap.get(p);
     360                    if (rel == null) {
    359361                        rel = new Relation((Relation) p);
    360362                        relationChangeMap.put((Relation) p, rel);
     
    362364                } else {
    363365                    rel = new Relation((Relation) p);
    364                     relationCommands.add(new ChangeCommand(p, rel));
     366                    relationCommands.add(new ChangeCommand(p, rel)); // should not happen
    365367                }
    366368                for (int i = 0; i < rel.getMembersCount(); i++) {
     
    384386                if (createMultipolygon || !seg.getWayNodes().equals(source.getNodes())) {
    385387                    sourceCopy.setNodes(seg.getWayNodes());
    386                     commands.add(new ChangeCommand(source, sourceCopy));
     388                    if (!tagsToRemove.isEmpty()) {
     389                        commands.add(new ChangePropertyCommand(Collections.singleton(source), tagsToRemove));
     390                    }
     391                    if (!sourceCopy.getNodes().equals(source.getNodes()))
     392                        commands.add(new ChangeNodesCommand(source, sourceCopy.getNodes()));
    387393                }
    388394                foundOwnWay = true;
     
    398404            }
    399405        }
     406        sourceCopy.setNodes(null); // see #19885
    400407        if (!foundOwnWay) {
    401408            final Command deleteCommand = DeleteCommand.delete(Collections.singleton(source));
     
    413420    public static void updateCommandsWithRelations(List<Command> commands, Map<Relation, Relation> relationCache) {
    414421        for (Map.Entry<Relation, Relation> entry : relationCache.entrySet()) {
    415             commands.add(new ChangeCommand(entry.getKey(), entry.getValue()));
     422            Relation oldRel = entry.getKey();
     423            Relation newRel = entry.getValue();
     424            if (oldRel.getKeys().equals(newRel.getKeys())) {
     425                commands.add(new ChangeMembersCommand(oldRel, newRel.getMembers()));
     426                newRel.setMembers(null); // see #19885
     427            } else {
     428                commands.add(new ChangeCommand(oldRel, newRel)); // should not happen
     429            }
    416430        }
    417431    }
Note: See TracChangeset for help on using the changeset viewer.