From 281639c7791ef58c2a614c8c7b6aa646b409b513 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Wed, 28 Oct 2009 15:42:03 +0000 Subject: [PATCH] fixed potential memory leak when motes are removed during a simulation --- .../plugins/skins/LogVisualizerSkin.java | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/plugins/skins/LogVisualizerSkin.java b/tools/cooja/java/se/sics/cooja/plugins/skins/LogVisualizerSkin.java index 0a411b2e9..a68202ac0 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/skins/LogVisualizerSkin.java +++ b/tools/cooja/java/se/sics/cooja/plugins/skins/LogVisualizerSkin.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: LogVisualizerSkin.java,v 1.3 2009/08/27 13:59:47 fros4943 Exp $ + * $Id: LogVisualizerSkin.java,v 1.4 2009/10/28 15:42:03 fros4943 Exp $ */ package se.sics.cooja.plugins.skins; @@ -43,6 +43,7 @@ import org.apache.log4j.Logger; import se.sics.cooja.ClassDescription; import se.sics.cooja.Mote; import se.sics.cooja.Simulation; +import se.sics.cooja.SimEventCentral.MoteCountListener; import se.sics.cooja.interfaces.Log; import se.sics.cooja.interfaces.Position; import se.sics.cooja.plugins.Visualizer; @@ -67,17 +68,18 @@ public class LogVisualizerSkin implements VisualizerSkin { visualizer.repaint(); } }; - private Observer simObserver = new Observer() { - public void update(Observable obs, Object obj) { - - /* Observe logs */ - for (Mote mote: simulation.getMotes()) { - Log log = mote.getInterfaces().getLog(); - if (log != null) { - log.addObserver(logObserver); - } + private MoteCountListener newMotesListener = new MoteCountListener() { + public void moteWasAdded(Mote mote) { + Log log = mote.getInterfaces().getLog(); + if (log != null) { + log.addObserver(logObserver); + } + } + public void moteWasRemoved(Mote mote) { + Log log = mote.getInterfaces().getLog(); + if (log != null) { + log.deleteObserver(logObserver); } - visualizer.repaint(); } }; @@ -85,17 +87,16 @@ public class LogVisualizerSkin implements VisualizerSkin { this.simulation = simulation; this.visualizer = vis; - simulation.addObserver(simObserver); - simObserver.update(null, null); + simulation.getEventCentral().addMoteCountListener(newMotesListener); + for (Mote m: simulation.getMotes()) { + newMotesListener.moteWasAdded(m); + } } public void setInactive() { - simulation.deleteObserver(simObserver); - for (Mote mote: simulation.getMotes()) { - Log log = mote.getInterfaces().getLog(); - if (log != null) { - log.deleteObserver(logObserver); - } + simulation.getEventCentral().removeMoteCountListener(newMotesListener); + for (Mote m: simulation.getMotes()) { + newMotesListener.moteWasRemoved(m); } }