diff --git a/js/energy-skate-park-basics/view/EnergySkateParkBasicsScreenView.js b/js/energy-skate-park-basics/view/EnergySkateParkBasicsScreenView.js index 3c6d2975..99b6edd1 100644 --- a/js/energy-skate-park-basics/view/EnergySkateParkBasicsScreenView.js +++ b/js/energy-skate-park-basics/view/EnergySkateParkBasicsScreenView.js @@ -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 ); diff --git a/js/energy-skate-park-basics/view/TrackNode.js b/js/energy-skate-park-basics/view/TrackNode.js index f0637771..2ba4734e 100644 --- a/js/energy-skate-park-basics/view/TrackNode.js +++ b/js/energy-skate-park-basics/view/TrackNode.js @@ -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 ], @@ -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() {