0

I have a memory leak in my application resulting from elements scopes not being destroyed.

For the following for field:

   vm.fields.data.directions = [
  {
    type: 'required-textarea',
    key: 'step',
    templateOptions:{
      label: 'Directions',
      placeholder: 'Wrap a bacon piece around each spear; secure ends with a toothpick.'
    },
    expressionProperties: {
      'link' : function($viewValue, $modelValue, scope){
        scope.$on('$destroy', function () {
          $timeout(function() {
            console.log(scope);
          }, 100);
        });
      }
    }

This consoles out a scope completely intact with multiple watchers. For my application I have multiple tabs for different form field inputs, some contain around 20 of the above referenced fields. Every time a tab is navigated away and clicked on again, the apps watcher count continues to grow since the scope is never destroyed.

Why would the scope exist here after destroyed is being called?

byrdr
  • 5,197
  • 12
  • 48
  • 78

1 Answers1

0

There is a problem with your test logic: at the time of the '$destroy' event, the scope has not yet been destroyed, so it will always appear in your console log.

From the docs:

Just before a scope is destroyed, a $destroy event is broadcasted on this scope. Application code can register a $destroy event handler that will give it a chance to perform any necessary cleanup.

The fact that your console log code is firing indicates that the scope is, in fact, being destroyed.

Shaun Scovil
  • 3,905
  • 5
  • 39
  • 58
  • The scope destroyed is true; however all of the watchers exist and are never removed for the life of the app. – byrdr Jan 28 '16 at 15:43
  • How are you determining that? Have you tried this script: https://medium.com/@kentcdodds/counting-angularjs-watchers-11c5134dc2ef#.l3h1whyzg – Shaun Scovil Jan 28 '16 at 15:44
  • I've added a timeout just to be sure. I'm watching it in batarang. The watcher count increases by the same number of watchers that are consoled out in the scope that is being destroyed. – byrdr Jan 28 '16 at 15:49
  • This SO answer might shed some light on the watcher situation: http://stackoverflow.com/a/25114028/630544 -- most likely, watchers are being added to the $parent scope and those are not being removed. You could do so in your '$destroy' event handler, if you can figure out what is being added that isn't getting removed. – Shaun Scovil Jan 28 '16 at 15:54