From 886b963ce5ae0ff0259adf97056245ca869133cc Mon Sep 17 00:00:00 2001 From: ahocevar Date: Sat, 9 Feb 2008 18:51:48 +0000 Subject: [PATCH] Fixed several destroy() methods to avoid running into undefined objects when the page unloads. r=crschmidt,me (closes #1222) git-svn-id: http://svn.openlayers.org/trunk/openlayers@6172 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Control.js | 1 + lib/OpenLayers/Control/Panel.js | 12 +++++++----- lib/OpenLayers/Map.js | 12 ++++++------ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/OpenLayers/Control.js b/lib/OpenLayers/Control.js index 03b44aac47..937c8edb46 100644 --- a/lib/OpenLayers/Control.js +++ b/lib/OpenLayers/Control.js @@ -166,6 +166,7 @@ OpenLayers.Control = OpenLayers.Class({ */ destroy: function () { this.events.destroy(); + this.events = null; // eliminate circular references if (this.handler) { this.handler.destroy(); diff --git a/lib/OpenLayers/Control/Panel.js b/lib/OpenLayers/Control/Panel.js index 15a445960e..638d20e126 100644 --- a/lib/OpenLayers/Control/Panel.js +++ b/lib/OpenLayers/Control/Panel.js @@ -45,11 +45,13 @@ OpenLayers.Control.Panel = OpenLayers.Class(OpenLayers.Control, { destroy: function() { OpenLayers.Control.prototype.destroy.apply(this, arguments); for(var i = this.controls.length - 1 ; i >= 0; i--) { - this.controls[i].events.un({ - "activate": this.redraw, - "deactivate": this.redraw, - scope: this - }); + if(this.controls[i].events) { + this.controls[i].events.un({ + "activate": this.redraw, + "deactivate": this.redraw, + scope: this + }); + } OpenLayers.Event.stopObservingElement(this.controls[i].panel_div); this.controls[i].panel_div = null; } diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index 62fb1ad5dc..fae32f8474 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -485,6 +485,12 @@ OpenLayers.Map = OpenLayers.Class({ this.events.unregister("resize", this, this.updateSize); } + if (this.controls != null) { + for (var i = this.controls.length - 1; i>=0; --i) { + this.controls[i].destroy(); + } + this.controls = null; + } if (this.layers != null) { for (var i = this.layers.length - 1; i>=0; --i) { //pass 'false' to destroy so that map wont try to set a new @@ -493,12 +499,6 @@ OpenLayers.Map = OpenLayers.Class({ } this.layers = null; } - if (this.controls != null) { - for (var i = this.controls.length - 1; i>=0; --i) { - this.controls[i].destroy(); - } - this.controls = null; - } if (this.viewPortDiv) { this.div.removeChild(this.viewPortDiv); }