Skip to content

Commit 0de98d9

Browse files
authored
Fix slow memory leak caused by not clearing cache (select2#5965)
There was a very slow memory leak that can occur if you were creating and destroying many Select2 elements on a single page. This was caused by the internal Select2 cache not being cleared for the container element, causing a reference to it to be left in memory even after the Select2 element was destroyed. The memory was put in place to make it easier to destroy a Select2 element after it is initialized, specifically when the user clicks off of the Select2 element. Because some pages run with multiple versions of Select2 and jQuery at the same time, we specifically cache a local copy of the jQuery element to allow it to be deconflicted at the time of the call. This cached locally copy was not being properly cleared when Select2 was being destroyed though, which had the (very small) potential to cause a memory leak over time.
1 parent a16a268 commit 0de98d9

1 file changed

Lines changed: 1 addition & 0 deletions

File tree

src/js/select2/core.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ define([
554554
};
555555

556556
Select2.prototype.destroy = function () {
557+
Utils.RemoveData(this.$container[0]);
557558
this.$container.remove();
558559

559560
this._observer.disconnect();

0 commit comments

Comments
 (0)