From 84e36dd57367fdbda9c82c9eba5228f80f625faa Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 4 Feb 2013 15:46:45 -0700 Subject: [PATCH] Avoid touching the geolocation object until needed IE9 leaks when `navigator.geolocation` is accessed (see #461). The goal of this change is to avoid that leak in builds that include the Geolocate control but do not use it. --- lib/OpenLayers/Control/Geolocate.js | 13 ++++++++++++- tests/Control/Geolocate.html | 12 ------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/OpenLayers/Control/Geolocate.js b/lib/OpenLayers/Control/Geolocate.js index f2fcb91910..4b5b43958e 100644 --- a/lib/OpenLayers/Control/Geolocate.js +++ b/lib/OpenLayers/Control/Geolocate.js @@ -45,8 +45,15 @@ OpenLayers.Control.Geolocate = OpenLayers.Class(OpenLayers.Control, { /** * Property: geolocation * {Object} The geolocation engine, as a property to be possibly mocked. + * This is set lazily to avoid a memory leak in IE9. */ - geolocation: navigator.geolocation, + geolocation: null, + + /** + * Property: available + * {Boolean} The navigator.geolocation object is available. + */ + available: ('geolocation' in navigator), /** * APIProperty: bind @@ -90,6 +97,10 @@ OpenLayers.Control.Geolocate = OpenLayers.Class(OpenLayers.Control, { * {Boolean} The control was effectively activated. */ activate: function () { + if (this.available && !this.geolocation) { + // set lazily to avoid IE9 memory leak + this.geolocation = navigator.geolocation; + } if (!this.geolocation) { this.events.triggerEvent("locationuncapable"); return false; diff --git a/tests/Control/Geolocate.html b/tests/Control/Geolocate.html index aa12c0452d..4e43f39945 100644 --- a/tests/Control/Geolocate.html +++ b/tests/Control/Geolocate.html @@ -101,18 +101,6 @@ map.removeControl(control); map.setCenter(centerLL); } - function test_uncapable(t) { - t.plan(1); - var control = new OpenLayers.Control.Geolocate({ - geolocation: null, - bind: false - }); - control.events.register('locationuncapable', null, function() { - t.ok(true,'uncapable browser fired locationuncapable event'); - }); - map.addControl(control); - control.activate(); - } function test_destroy(t) { t.plan(1); var control = new OpenLayers.Control.Geolocate({