On Github eshcharc / memory-leaking-presentation
return function alCaponeLink(scope, element, attrs) {
(function perform100Robberies() {
var div,
i;
for(i = 0; i < 1000; i+=1) {
div = $window.document.createElement('div');
$window.alCaponeTreasure.push(div);
}
})();
scope.$on('$destroy', function findCaponeTreasure() {
$window.alCaponeTreasure = null; // <- This fixes the leak
});
};
function timeBombDirective () {
return function timeBombLnk(scope, element, attrs) {
(function setTimeBomb() {
element.datepicker();
})();
scope.$on('$destroy', function neutralizeTimeBomb() {
element.datepicker('destroy'); // <- This fixes the leak
});
};
}
function tedBundyDirective ($interval) {
var Body = function () {
// define person
};
return function tedBundyLink(scope, element, attrs) {
var bodies = [],
killingInterval = $interval(function kill() {
bodies.push(new Body());
}, 50);
scope.$on('$destroy', function arrestTedBundy() {
$interval.cancel(killingInterval); // <- This fixes the leak
})
};
}