Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node's dispose isn't called often #601

Closed
jonathanolson opened this issue Feb 4, 2017 · 9 comments
Closed

Node's dispose isn't called often #601

jonathanolson opened this issue Feb 4, 2017 · 9 comments
Assignees

Comments

@jonathanolson
Copy link
Contributor

A ton of types are overriding dispose() on Node and not calling the Node.prototype.dispose(). It has always existed, but was just part of Events which had a no-op dispose() method, but now (with the Tandem handling) it actually does something.

Flagging for developer meeting for handling. I'll check to see whether I can identify where this happens by toString()ing functions automatically.

@samreid
Copy link
Member

samreid commented Feb 9, 2017

I added in the Node.prototype.dispose.call(this) into many of the instrumented sun and scenery-phet classes. It would be nice to check that other node subclasses address this.

@pixelzoom
Copy link
Contributor

pixelzoom commented Feb 9, 2017

I've started using this pattern to future-proof implementations of dispose that involve a supertype:

dispose: function() {
  SuperType.prototype.dispose && SuperType.prototype.dispose.call( this );
  this.disposeSubType();
}

The advantage of the above approach is that I don't have to go hunting for dispose functions in the type hierarchy -- which are sometime not at all easy to find, due to mixins or non-intutivie hierarchies (Node extends Events being a prime example).

The disadvantage of the above approach is that I don't identify supertypes that should have a dispose function and are therefore leaking memory.

Another disadvantage is that, a future implementation of the supertype's dispose may break my code. So when you add a dispose function to a type, don't assume that you don't need to test! There may be code that starts to use your new dispose function as soon as it's added.

@samreid
Copy link
Member

samreid commented Feb 9, 2017

Generally, the order should be reversed:

dispose: function() {
  this.disposeSubType();
  SuperType.prototype.dispose && SuperType.prototype.dispose.call( this );
}

That is, the order an object is disassembled should be the reverse of the order the object is assembled. For instance, if subType does this.children.push(x) and subType dispose does this.children.remove(x), that must be done before the SuperType eliminates the children array.

@pixelzoom pixelzoom self-assigned this Feb 9, 2017
@pixelzoom
Copy link
Contributor

Self assigned to look at dispose implementations in my sims to see if any of them need to be fixed, specifically related to Node.

@pixelzoom
Copy link
Contributor

pixelzoom commented Feb 9, 2017

@jonathanolson said:

I'll check to see whether I can identify where this happens by toString()ing functions automatically.

In 2/9/17 dev meeting, he said there's a better way to do this, and he'll do it.

pixelzoom added a commit to phetsims/beers-law-lab that referenced this issue Feb 9, 2017
pixelzoom added a commit to phetsims/balancing-chemical-equations that referenced this issue Feb 9, 2017
pixelzoom added a commit to phetsims/griddle that referenced this issue Feb 9, 2017
pixelzoom added a commit to phetsims/joist that referenced this issue Feb 9, 2017
pixelzoom added a commit to phetsims/reactants-products-and-leftovers that referenced this issue Feb 9, 2017
pixelzoom added a commit to phetsims/unit-rates that referenced this issue Feb 9, 2017
pixelzoom added a commit to phetsims/vegas that referenced this issue Feb 9, 2017
pixelzoom added a commit to phetsims/sun that referenced this issue Feb 9, 2017
pixelzoom added a commit to phetsims/axon that referenced this issue Feb 9, 2017
@pixelzoom
Copy link
Contributor

pixelzoom commented Feb 9, 2017

I took care of my sims, and all common-code except:

  • scenery-phet/js/*.js
  • scenery-phet/js/buttons/*.js
  • sun/js/*.js

I intended to handle these sometime in the next week.

pixelzoom added a commit to phetsims/sun that referenced this issue Feb 10, 2017
pixelzoom added a commit to phetsims/scenery-phet that referenced this issue Feb 10, 2017
@pixelzoom
Copy link
Contributor

pixelzoom commented Feb 10, 2017

I completed common-code, see above commits.
Tested using test-server with ?ea&testBuilt=false&fuzzMouse.

@pixelzoom pixelzoom removed their assignment Feb 10, 2017
@pixelzoom
Copy link
Contributor

pixelzoom commented Feb 10, 2017

All devs: Highly recommended to address this issue in your sims. You can wait until @jonathanolson implements automated checking. Or you can manually inspect dispose functions to verify that they chain to their supertype’s dispose function (search for dispose:). In either case, here's my recommendation on how to address this issue in client code.

First, identify whether the supertype has a dispose function. The easiest way to do this is to run a sim, then inspect the supertype's prototype in the console.

For example, Text does have a dispose function, while Color does not:

> phet.scenery.Text.prototype.dispose
function () { ... }
> phet.scenery.Color.prototype.dispose
undefined

If the supertype does have a dispose function, then override dispose like this:

dispose: function() {
  this.disposeSubType();
  SuperType.prototype.dispose.call( this );
}

If the supertype does not have a dispose function, then I recommend future-proofing your code by overriding dispose like this:

dispose: function() {
  this.disposeSubType();
  SuperType.prototype.dispose && SuperType.prototype.dispose.call( this );
}

jonathanolson added a commit to phetsims/build-an-atom that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/charges-and-fields that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/circuit-construction-kit-common that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/curve-fitting that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/expression-exchange that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/function-builder that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/isotopes-and-atomic-mass that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/least-squares-regression that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/models-of-the-hydrogen-atom that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/molecules-and-light that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/proportion-playground that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/rutherford-scattering that referenced this issue Feb 22, 2017
jonathanolson added a commit that referenced this issue Feb 22, 2017
Checks for repeated disposal or not calling Node.dispose()
jonathanolson added a commit that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/rutherford-scattering that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/expression-exchange that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/molecules-and-light that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/shred that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/circuit-construction-kit-common that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/expression-exchange that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/least-squares-regression that referenced this issue Feb 22, 2017
…rriding Node's dispose (and we wouldn't want to call that). See phetsims/scenery#601
jonathanolson added a commit to phetsims/rutherford-scattering that referenced this issue Feb 22, 2017
jonathanolson added a commit to phetsims/expression-exchange that referenced this issue Feb 22, 2017
@jonathanolson
Copy link
Contributor Author

Issues created for locations that still have this problem, and since it seems to be helpful long-term, I'll leave in the assertion checks to make sure Node.dispose() is called (and that it isn't called twice).

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

No branches or pull requests

3 participants