fixed potential memory leak when motes are removed during a simulation

This commit is contained in:
fros4943 2009-10-28 15:42:03 +00:00
parent a4226e14c5
commit 281639c779

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja.plugins.skins;
@ -43,6 +43,7 @@ import org.apache.log4j.Logger;
import se.sics.cooja.ClassDescription; import se.sics.cooja.ClassDescription;
import se.sics.cooja.Mote; import se.sics.cooja.Mote;
import se.sics.cooja.Simulation; import se.sics.cooja.Simulation;
import se.sics.cooja.SimEventCentral.MoteCountListener;
import se.sics.cooja.interfaces.Log; import se.sics.cooja.interfaces.Log;
import se.sics.cooja.interfaces.Position; import se.sics.cooja.interfaces.Position;
import se.sics.cooja.plugins.Visualizer; import se.sics.cooja.plugins.Visualizer;
@ -67,17 +68,18 @@ public class LogVisualizerSkin implements VisualizerSkin {
visualizer.repaint(); visualizer.repaint();
} }
}; };
private Observer simObserver = new Observer() { private MoteCountListener newMotesListener = new MoteCountListener() {
public void update(Observable obs, Object obj) { public void moteWasAdded(Mote mote) {
Log log = mote.getInterfaces().getLog();
/* Observe logs */ if (log != null) {
for (Mote mote: simulation.getMotes()) { log.addObserver(logObserver);
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.simulation = simulation;
this.visualizer = vis; this.visualizer = vis;
simulation.addObserver(simObserver); simulation.getEventCentral().addMoteCountListener(newMotesListener);
simObserver.update(null, null); for (Mote m: simulation.getMotes()) {
newMotesListener.moteWasAdded(m);
}
} }
public void setInactive() { public void setInactive() {
simulation.deleteObserver(simObserver); simulation.getEventCentral().removeMoteCountListener(newMotesListener);
for (Mote mote: simulation.getMotes()) { for (Mote m: simulation.getMotes()) {
Log log = mote.getInterfaces().getLog(); newMotesListener.moteWasRemoved(m);
if (log != null) {
log.deleteObserver(logObserver);
}
} }
} }