Cleaning up map layers when map.remove() is run
This commit is contained in:
parent
7bc37fe09a
commit
81b038926c
56
debug/tests/detached-dom-memory-leak.html
Normal file
56
debug/tests/detached-dom-memory-leak.html
Normal file
@ -0,0 +1,56 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Leaflet debug page</title>
|
||||
<link rel="stylesheet" href="../../dist/leaflet.css" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="../css/screen.css" />
|
||||
<script type="text/javascript" src="../../build/deps.js"></script>
|
||||
<script src="../leaflet-include.js"></script>
|
||||
<script>
|
||||
var map,
|
||||
mapDiv,
|
||||
osm = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png');
|
||||
|
||||
|
||||
recreateMap = function(){
|
||||
// destroy previous map and div
|
||||
|
||||
if(map) map.remove(); // This will destroy all DOM childs from layers and controls
|
||||
if(mapDiv) mapDiv.parentNode.removeChild(mapDiv); // This will destroy the map div
|
||||
|
||||
// create new map div
|
||||
var randomDivId = 'mapId' + new Date().getTime();
|
||||
mapDiv = document.createElement('div');
|
||||
mapDiv.id = randomDivId;
|
||||
mapDiv.style.height = '200px';
|
||||
mapDiv.style.width = '200px';
|
||||
document.getElementsByTagName('body')[0].appendChild(mapDiv);
|
||||
// attach map to div
|
||||
map = L.map(randomDivId).setView([51.505, -0.09], 13);
|
||||
map.addLayer(osm);
|
||||
};
|
||||
|
||||
var interval = null;
|
||||
function start(){
|
||||
interval = window.setInterval(recreateMap, 200);
|
||||
}
|
||||
|
||||
function stop() {
|
||||
window.clearInterval(interval);
|
||||
}
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
This page will destroy and recreate a map div lots of times. Developer tools shall not display a memory leak.
|
||||
|
||||
<div>
|
||||
<button onclick='recreateMap();'>Once</button>
|
||||
<button onclick='start();'>Start</button>
|
||||
<button onclick='stop();'>Stop</button>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
@ -272,6 +272,10 @@ L.Map = L.Evented.extend({
|
||||
this.fire('unload');
|
||||
}
|
||||
|
||||
for (var i in this._layers) {
|
||||
this._layers[i].remove();
|
||||
}
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user