BossView icon indicating copy to clipboard operation
BossView copied to clipboard

Call destroy on parent doesn't destroy the subviews.

Open SirbyAlive opened this issue 10 years ago • 1 comments

Problem :

Calling destroy on BossView object doesn't call destroy on subviews, resulting on a potential memory leak.

Subviews' "onDestroy" never get called, and in the Chrome Backbone Debugger we can see that the subviews get destroyed, in a way (?!)(but this debugger is not always accurate), but all their own children are kept alive.


Solution :

I've added destroy as the remove call

destroy: function () {
  Marionette.ItemView.prototype.destroy.apply(this, arguments);
  this._destroySubViews();
},

_destroySubViews: function () {
  _.each(this.initializedSubViews, function(subView) {
    subView.destroy();
  });
}

It does the trick for me but I don't have the time to make the unit test and a pull request. Add if you think it's relevant.

SirbyAlive avatar Oct 10 '14 08:10 SirbyAlive

Also found this problem when was hunting down huge memory leaks. Here's my simplier fix:

onDestroy: function onDestroy() {
    _.invoke(this.initializedSubViews, 'destroy');
}

Desp163 avatar Nov 02 '15 22:11 Desp163