Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit 993dfd7

Browse files
committed
DTInstance.getList() possible memory leak #284
1 parent 7c38bc6 commit 993dfd7

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

dist/angular-datatables.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ angular.module('datatables.instances', ['datatables.util'])
408408
.factory('DTInstanceFactory', dtInstanceFactory);
409409

410410
/* @ngInject */
411-
function dtInstances($q, failzQ) {
411+
function dtInstances($q, failzQ, $timeout) {
412+
var TIME_BEFORE_CLEANING = 1000;
412413
var _instances = {};
413414
var _lastInstance = {};
414415
// Promise for fetching the last DT instance
@@ -428,7 +429,9 @@ function dtInstances($q, failzQ) {
428429
dtInstance.DataTable = result.DataTable;
429430
dtInstance.dataTable = result.dataTable;
430431

432+
//_instances[dtInstance.id] = dtInstance;
431433
_instances[dtInstance.id] = dtInstance;
434+
_cleanInstances();
432435
_lastInstance = dtInstance;
433436
if (_deferLastDTInstances) {
434437
_deferLastDTInstances.resolve(_lastInstance);
@@ -474,8 +477,22 @@ function dtInstances($q, failzQ) {
474477
});
475478
return defer.promise;
476479
}
480+
481+
function _cleanInstances() {
482+
$timeout(function() {
483+
var newInstances = {};
484+
for (var attr in _instances) {
485+
if (_instances.hasOwnProperty(attr)) {
486+
if ($.fn.DataTable.isDataTable(_instances[attr].id)) {
487+
newInstances[attr] = _instances[attr];
488+
}
489+
}
490+
}
491+
_instances = newInstances;
492+
}, TIME_BEFORE_CLEANING);
493+
}
477494
}
478-
dtInstances.$inject = ['$q', 'failzQ'];
495+
dtInstances.$inject = ['$q', 'failzQ', '$timeout'];
479496

480497
function dtInstanceFactory() {
481498
var DTInstance = {

0 commit comments

Comments
 (0)