Closed phet-steele closed 6 years ago
Somehow rearranging the derived property disposals seems to help.......
I'm puzzled, but reordering the derived property disposal changes whether there is an assertion or not. I also cleaned up DerivedProperty.dispose in https://github.com/phetsims/axon/issues/148
@zepumph and/or @jbphet can you please take a look around?
I'll take a look.
I tried for a bit to investigate this, and I did it by manually reverting the change to the order of disposal of properties in PropertyAtom.dispose()
. This didn't cause any problems in operation of the non-phet-io version as far as I could tell with quick testing, nor with the phet-io state wrapper or instance-proxies wrapper. However, after thinking about it, I suspect none of these invoke the PropertyAtom.dispose()
method, so I tried the state wrapper, but ran into other problems there (the message I hit was mismatched messageIndex, possible start/end mismatch
). @samreid and I have a collaboration session scheduled for the near future, perhaps we should take a look together then.
@samreid, @zepumph, and I dug into this, and it now makes sense to us why it is necessary to dispose derived properties before their dependencies. The reason is that if a dependency is disposed before a derived property, the listener function that is linked to the dependency by the derived property (to allow the derived property to update when the dependency changes) will be removed, then when the derived property is disposed, it will attempt to remove that same listener function, which will cause an assert due to recent changes to axon that flags attempts to remove listeners that aren't present (see https://github.com/phetsims/axon/issues/129 and https://github.com/phetsims/axon/issues/132).
We were wondering why this problem didn't show up as soon as the mods to axon were made, and it is probably because there are a lot of derived properties that weren't disposed because they were unlikely to cause memory leaks. However, in the process of instrumenting the game for Build an Atom and getting state save/restore to work, there is a lot of additional disposal that is needing to be done. This is causing this case to be hit a lot.
Marking for developer meeting to discuss so that developers are aware of this constraint in the way the disposal occurs.
Here is the example that exposed this problem from ParticleAtom.js:
dispose: function() {
/* DerivedProperties that use the lower countProperties */
this.particleCountProperty.dispose();
this.massNumberProperty.dispose();
this.chargeProperty.dispose();
// These should be disposed after because they are dependencies to the above DerivedProperties
this.protonCountProperty.dispose();
this.neutronCountProperty.dispose();
this.electronCountProperty.dispose();
. . . .
}
This has been fixed - closing.
In levels 2 - 4, try to complete challenges that look like this (a
SchematicToSymbolChallenge
):