Skip to content

Commit

Permalink
Fix memory leak when removing map (#13110)
Browse files Browse the repository at this point in the history
* 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 <stepan.kuzmin@mapbox.com>
  • Loading branch information
kamil-sienkiewicz-asi and stepankuzmin committed Mar 14, 2024
1 parent d6eacf6 commit 22e183b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/style/style.js
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion src/ui/map.js
Expand Up @@ -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';
Expand Down Expand Up @@ -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'));
}
Expand Down
9 changes: 7 additions & 2 deletions src/util/mapbox.js
Expand Up @@ -576,6 +576,11 @@ export class MapSessionAPI extends TelemetryEvent {
}
}, customAccessToken);
}

remove() {
// $FlowFixMe[incompatible-type]
this.errorCb = null;
}
}

export class TurnstileEvent extends TelemetryEvent {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 22e183b

Please sign in to comment.