From 22e183bcede2594a2da5ca0a87a4548e5bb38fa4 Mon Sep 17 00:00:00 2001 From: Kamil Sienkiewicz <138137613+kamil-sienkiewicz-asi@users.noreply.github.com> Date: Thu, 14 Mar 2024 16:23:52 +0100 Subject: [PATCH] Fix memory leak when removing map (#13110) * chore: call _remove on fragment styles when destroing Style * chore: apply code styling * Cleanup MapSessionAPI error callback on map.remove --------- Co-authored-by: Stepan Kuzmin --- src/style/style.js | 3 +++ src/ui/map.js | 5 ++++- src/util/mapbox.js | 9 +++++++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/style/style.js b/src/style/style.js index 8067c79df50..631a5d2a2c4 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -3037,6 +3037,9 @@ class Style extends Evented { destroy() { this._clearWorkerCaches(); + this.fragments.forEach(fragment => { + fragment.style._remove(); + }); if (this.terrainSetForDrapingOnly()) { delete this.terrain; delete this.stylesheet.terrain; diff --git a/src/ui/map.js b/src/ui/map.js index 827ff84c181..5dedc0bb661 100755 --- a/src/ui/map.js +++ b/src/ui/map.js @@ -5,7 +5,7 @@ import {asyncAll, extend, bindAll, warnOnce, uniqueId, isSafariWithAntialiasingB import browser from '../util/browser.js'; import * as DOM from '../util/dom.js'; import {getImage, getJSON, ResourceType} from '../util/ajax.js'; -import {RequestManager, getMapSessionAPI, postPerformanceEvent, postMapLoadEvent, AUTH_ERR_MSG, storeAuthState, removeAuthState} from '../util/mapbox.js'; +import {RequestManager, mapSessionAPI, getMapSessionAPI, postPerformanceEvent, postMapLoadEvent, AUTH_ERR_MSG, storeAuthState, removeAuthState} from '../util/mapbox.js'; import Style from '../style/style.js'; import EvaluationParameters from '../style/evaluation_parameters.js'; import Painter from '../render/painter.js'; @@ -4012,6 +4012,9 @@ class Map extends Camera { PerformanceUtils.clearMetrics(); removeAuthState(this.painter.context.gl); + + mapSessionAPI.remove(); + this._removed = true; this.fire(new Event('remove')); } diff --git a/src/util/mapbox.js b/src/util/mapbox.js index 8e346017baa..9c1faf8b9eb 100644 --- a/src/util/mapbox.js +++ b/src/util/mapbox.js @@ -576,6 +576,11 @@ export class MapSessionAPI extends TelemetryEvent { } }, customAccessToken); } + + remove() { + // $FlowFixMe[incompatible-type] + this.errorCb = null; + } } export class TurnstileEvent extends TelemetryEvent { @@ -660,9 +665,9 @@ export const performanceEvent_: PerformanceEvent = new PerformanceEvent(); // $FlowFixMe[method-unbinding] export const postPerformanceEvent: (?string, LivePerformanceData) => void = performanceEvent_.postPerformanceEvent.bind(performanceEvent_); -const mapSessionAPI_ = new MapSessionAPI(); +export const mapSessionAPI: MapSessionAPI = new MapSessionAPI(); // $FlowFixMe[method-unbinding] -export const getMapSessionAPI: (number, string, ?string, EventCallback) => void = mapSessionAPI_.getSessionAPI.bind(mapSessionAPI_); +export const getMapSessionAPI: (number, string, ?string, EventCallback) => void = mapSessionAPI.getSessionAPI.bind(mapSessionAPI); const authenticatedMaps = new Set(); export function storeAuthState(gl: WebGL2RenderingContext, state: boolean) {