Skip to content

Commit

Permalink
dispose TrackNodes for #399
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegreenberg committed May 30, 2018
1 parent f4ab884 commit d67e9c6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ define( function( require ) {
var itemRemovedListener = function( removed ) {
if ( removed === track ) {
trackLayer.removeChild( trackNode );
model.tracks.removeItemRemovedListener( itemRemovedListener );// Clean up memory leak

// Clean up memory leak
model.tracks.removeItemRemovedListener( itemRemovedListener );
trackNode.dispose();
}
};
model.tracks.addItemRemovedListener( itemRemovedListener );
Expand Down
21 changes: 19 additions & 2 deletions js/energy-skate-park-basics/view/TrackNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ define( function( require ) {
lineDash: [ 11, 8 ],
tandem: tandem.createTandem( 'centerLineNode' )
} );
model.detachableProperty.link( function( detachable ) {

// must be unlinked in dispose
var detachableListener = function( detachable ) {
self.centerLine.lineDash = detachable ? [] : [ 11, 8 ];
} );
};
model.detachableProperty.link( detachableListener );

Node.call( this, {
children: [ this.road, this.centerLine ],
Expand Down Expand Up @@ -108,12 +111,26 @@ define( function( require ) {
phet.phetIo && phet.phetIo.phetio.setStateEmitter && phet.phetIo.phetio.setStateEmitter.addListener( function() {
self.updateTrackShape();
} );

// @private - only called by dispose
this.disposeTrackNode = function() {
model.detachableProperty.unlink( detachableListener );
};
}

energySkateParkBasics.register( 'TrackNode', TrackNode );

return inherit( Node, TrackNode, {

/**
* Make eligible for garbage collection.
* @public
*/
dispose: function() {
this.disposeTrackNode();
Node.prototype.dispose.call( this );
},

// When a control point has moved, or the track has moved, or the track has been reset, or on initialization
// update the shape of the track.
updateTrackShape: function() {
Expand Down

0 comments on commit d67e9c6

Please sign in to comment.