diff --git a/lib/OpenLayers/Layer/Grid.js b/lib/OpenLayers/Layer/Grid.js index e718bde549..fe7a179cd2 100644 --- a/lib/OpenLayers/Layer/Grid.js +++ b/lib/OpenLayers/Layer/Grid.js @@ -73,6 +73,10 @@ OpenLayers.Layer.Grid.prototype = Object.extend( new OpenLayers.Layer(), { //first of all, clear out the main div this.div.innerHTML = ""; + //now clear out the old grid and start a new one + this.clearGrid(); + this.grid = new Array(); + var viewSize = this.map.getSize(); var bounds = this.map.getExtent(); var extent = this.map.getFullExtent(); @@ -96,7 +100,6 @@ OpenLayers.Layer.Grid.prototype = Object.extend( new OpenLayers.Layer(), { tileoffsety = Math.round(tileoffsety); this.origin = new OpenLayers.Pixel(tileoffsetx,tileoffsety); - this.grid = new Array(); var startX = tileoffsetx; var startLon = tileoffsetlon; @@ -223,5 +226,23 @@ OpenLayers.Layer.Grid.prototype = Object.extend( new OpenLayers.Layer(), { } } return requestString; + }, + + /** go through and remove all tiles from the grid, calling + * destroy() on each of them to kill circular references + * + * @private + */ + clearGrid:function() { + while(grid.length > 0) { + var row = grid[0]; + while(row.length > 0) { + var tile = row[0]; + tile.destroy(); + row.remove(tile); + } + grid.remove(row); + } } + }); diff --git a/lib/OpenLayers/Tile.js b/lib/OpenLayers/Tile.js index 156912a078..291deff1af 100644 --- a/lib/OpenLayers/Tile.js +++ b/lib/OpenLayers/Tile.js @@ -42,6 +42,14 @@ OpenLayers.Tile.prototype = { this.size = size; } }, + + /** nullify references to prevent circular references and memory leaks + */ + destroy:function() { + this.grid = null; + this.bounds = null; + this.size = null; + }, /** */