Fix memory leak when the dropdown is opened #2566
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This pull request fixes a memory leak in select2 which is caused by the event handler (link to code) on the dropdown mask, which once created is never destroyed.
After opening the select2, the above event handler is registered. The problem is that it references the parent context of the opening function, so that context is never released. And the
that
variable (which holds thethis
) is promoted to the context because it is referenced in this closure. This kind of memory leak is described in more detail here and here.I've reproduced the leak in this plunker (direct link), where after opening the select2 and destroying it there is still a reference to BigObject, which is held by the event handler:
The heap snapshot was taken using Chrome 36 in incognito mode.
Solution
The memory leak is fixed by creating the event handler in a separate function. This plunker (direct link) is using the fixed version of the code, so you can see that everything is OK.