From 0b8bc8448af0fffd3e377c311e5cadb0dc851ca9 Mon Sep 17 00:00:00 2001 From: Zukero Date: Wed, 28 Feb 2018 16:25:28 +0100 Subject: [PATCH] Fixed memory leaks. Closed projects were held in memory because the Map folder watcher threads were still alive. --- .../atcontentstudio/model/maps/TMXMapSet.java | 27 ++++++++++--------- .../atcontentstudio/ui/WorkspaceActions.java | 1 + 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/com/gpl/rpg/atcontentstudio/model/maps/TMXMapSet.java b/src/com/gpl/rpg/atcontentstudio/model/maps/TMXMapSet.java index 1e14eb5..031cb42 100644 --- a/src/com/gpl/rpg/atcontentstudio/model/maps/TMXMapSet.java +++ b/src/com/gpl/rpg/atcontentstudio/model/maps/TMXMapSet.java @@ -15,6 +15,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.Enumeration; import java.util.List; +import java.util.concurrent.TimeUnit; import javax.swing.tree.TreeNode; @@ -102,19 +103,21 @@ public class TMXMapSet implements ProjectTreeNode { /*WatchKey watchKey = */folderPath.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY); WatchKey wk; validService: while(getProject().open) { - wk = watchService.take(); - for (WatchEvent event : wk.pollEvents()) { - Path changed = (Path) event.context(); - String name = changed.getFileName().toString(); - String id = name.substring(0, name.length() - 4); - TMXMap map = getMap(id); - if (map != null) { - map.mapChangedOnDisk(); + wk = watchService.poll(10, TimeUnit.SECONDS); + if (wk != null) { + for (WatchEvent event : wk.pollEvents()) { + Path changed = (Path) event.context(); + String name = changed.getFileName().toString(); + String id = name.substring(0, name.length() - 4); + TMXMap map = getMap(id); + if (map != null) { + map.mapChangedOnDisk(); + } + } + if(!wk.reset()) { + watchService.close(); + break validService; } - } - if(!wk.reset()) { - watchService.close(); - break validService; } } } catch (IOException e) { diff --git a/src/com/gpl/rpg/atcontentstudio/ui/WorkspaceActions.java b/src/com/gpl/rpg/atcontentstudio/ui/WorkspaceActions.java index af881d6..4ef50c6 100644 --- a/src/com/gpl/rpg/atcontentstudio/ui/WorkspaceActions.java +++ b/src/com/gpl/rpg/atcontentstudio/ui/WorkspaceActions.java @@ -59,6 +59,7 @@ public class WorkspaceActions { public void actionPerformed(ActionEvent e) { if (!(selectedNode instanceof Project)) return; Workspace.closeProject((Project) selectedNode); + selectedNode = null; }; public void selectionChanged(ProjectTreeNode selectedNode, TreePath[] selectedPaths) { setEnabled(selectedNode instanceof Project);