Skip to content

Avoiding closure leaks#14

Open
andrija-hers wants to merge 3 commits intoeinaros:masterfrom
andrija-hers:master
Open

Avoiding closure leaks#14
andrija-hers wants to merge 3 commits intoeinaros:masterfrom
andrija-hers:master

Conversation

@andrija-hers
Copy link
Copy Markdown

Here is a possible solution for avoiding massive memory leaks that occur via closures.
These leaks happen for each property built on the values variable. In other words, for every property of the defaults object, a leaking closure is created.
In order to overcome such a behavior, the destroy method is introduced on the Options object.
Proposed usage:

var options = new Options({width: 1, height: 1});
...
options.destroy();

However, in certain use cases, a developer might find useful to "forget" about the Options object and continue using its values property object. In such a situation it would be clumsy to drag the reference to the original Options object in order to destroy it eventually.
For such cases, another helper is introduced - the destroymethodname.
Proposed usage:

function createValues (defaults) {
  var options = new Options(defaults, 'dispose');
  return options.values;
}
function useOptions () {
  var values = createValues({width: 1, height: 1});
  ...
  values.dispose();
}

The PR proposed is backwards-compatible - but one should bare in mind that without calling destroy on the Options object or the destroymethodname on its values property, the resulting memory leaks will remain in massive amounts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant