Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling map remove method does not remove FullscreenControl event listener #7036

Closed
stephengsumo opened this issue Jul 26, 2018 · 5 comments
Closed

Comments

@stephengsumo
Copy link

Calling map.remove() does not remove Controls from the map. I'm using the FullScreen control and each time a new map is created, it adds a document event listener onfullscreenchange. This listener has a reference to the map and so memory is being leaked.

mapbox-gl-js version: 0.47.0

browser: Chrome Version 67.0.3396.99

Steps to Trigger Behavior

Go to https://jsfiddle.net/pmxgoqh3/8/

  1. In Chrome console, do getEventListeners(document). You'll see an array of only 1 listener.
  2. Press reload map link 5 times
  3. In Chrome console, do getEventListeners(document). You'll see the array of listeners go from 1 to 6 (5 more listeners than step 1).

Link to Demonstration

https://jsfiddle.net/pmxgoqh3/8/

Expected Behavior

Calling map.remove() should remove Controls from the map.

Actual Behavior

Calling map.remove() does not remove Controls from the map.

@mollymerp mollymerp changed the title Calling map remove method does not remove Controls Calling map remove method does not remove FullscreenControl event listener Jul 27, 2018
@mollymerp
Copy link
Contributor

I think this memory leak is only relevant to the FullscreenControl because the other controls only add event listeners on control-owned DOM nodes so they are garbage collected when the control container node is removed, but the FullscreenControl adds document level event listener. Because the map does not store a reference to the FullscreenControl instance, it is currently unable to remove the associated event listener when map.remove() is called.

A workaround for this issue pending a more permanent fix is to keep a reference to the control and call map.removeControl(fsControl) prior to calling map.remove()

@jfirebaugh
Copy link
Contributor

A workaround for this issue pending a more permanent fix is to keep a reference to the control and call map.removeControl(fsControl) prior to calling map.remove()

This sounds like the right fix to me. In fact, the map should call removeControl on all the controls that have been added with addControl, because someone might have added their own custom control that needs to do cleanup.

@stephengsumo
Copy link
Author

Yeah for now I am calling map.removeControl(fsControl) before map.remove(). I just assumed that map.remove() would remove all controls too. The API documentation doesn't say that controls are removed, but it also doesn't say we should remove controls before calling map.remove(). https://www.mapbox.com/mapbox-gl-js/api/#map#remove

@jfirebaugh
Copy link
Contributor

Oh, I think I misread @mollymerp's suggestion. She was suggesting an end-user workaround. I agree @stephengsumo, map.remove() should remove all controls too.

@mollymerp
Copy link
Contributor

closed by #7042

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants