From e2f4bb9ffce2c11ce06a33d275dffbda3d18bf07 Mon Sep 17 00:00:00 2001 From: Michael Kauzmann Date: Fri, 30 Aug 2024 09:59:06 -0600 Subject: [PATCH] More memory leaks!, https://github.com/phetsims/density-buoyancy-common/issues/168 Signed-off-by: Michael Kauzmann --- js/accessibility/nodes/WASDCueNode.ts | 33 +++++++++++++++++---------- js/keyboard/KeyNode.ts | 7 ++++-- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/js/accessibility/nodes/WASDCueNode.ts b/js/accessibility/nodes/WASDCueNode.ts index 6fc544e3..2f77a2c2 100644 --- a/js/accessibility/nodes/WASDCueNode.ts +++ b/js/accessibility/nodes/WASDCueNode.ts @@ -41,19 +41,18 @@ const DIRECTION_ANGLES = { }; export default class WASDCueNode extends Node { - protected wNode: Node; - protected aNode: Node; - protected sNode: Node; - protected dNode: Node; + protected readonly wNode: Node; + protected readonly aNode: Node; + protected readonly sNode: Node; + protected readonly dNode: Node; public constructor( boundsProperty: TReadOnlyProperty ) { - super(); - this.wNode = this.createMovementKeyNode( 'up' ); - this.aNode = this.createMovementKeyNode( 'left' ); - this.sNode = this.createMovementKeyNode( 'down' ); - this.dNode = this.createMovementKeyNode( 'right' ); + this.wNode = WASDCueNode.createMovementKeyNode( 'up' ); + this.aNode = WASDCueNode.createMovementKeyNode( 'left' ); + this.sNode = WASDCueNode.createMovementKeyNode( 'down' ); + this.dNode = WASDCueNode.createMovementKeyNode( 'right' ); const directionKeysParent = new Node( { children: [ @@ -66,22 +65,31 @@ export default class WASDCueNode extends Node { this.addChild( directionKeysParent ); - boundsProperty.link( bounds => { + const boundsListener = ( bounds: Bounds2 ) => { // place the direction cues relative to the object bounds this.wNode.centerBottom = bounds.getCenterTop().plusXY( 0, -KEY_SPACING ); this.aNode.rightCenter = bounds.getLeftCenter().plusXY( -KEY_SPACING, 0 ); this.sNode.centerTop = bounds.getCenterBottom().plusXY( 0, KEY_SPACING + SHADOW_OFFSET ); this.dNode.leftCenter = bounds.getRightCenter().plusXY( KEY_SPACING + SHADOW_OFFSET, 0 ); + }; + boundsProperty.link( boundsListener ); + + this.disposeEmitter.addListener( () => { + boundsProperty.unlink( boundsListener ); + + this.wNode.dispose(); + this.aNode.dispose(); + this.sNode.dispose(); + this.dNode.dispose(); } ); } - /** * Create a node that looks like a keyboard letter key next to an arrow indicating the direction the balloon * would move if that key is pressed. */ - private createMovementKeyNode( direction: 'up' | 'down' | 'left' | 'right' ): Node { + private static createMovementKeyNode( direction: 'up' | 'down' | 'left' | 'right' ): Node { // create the arrow icon const arrowShape = new Shape(); @@ -116,6 +124,7 @@ export default class WASDCueNode extends Node { } assert && assert( box!, `No box created for direction ${direction}` ); + box!.disposeEmitter.addListener( () => keyIcon.dispose() ); return box!; } } diff --git a/js/keyboard/KeyNode.ts b/js/keyboard/KeyNode.ts index d582b94a..96ba359b 100644 --- a/js/keyboard/KeyNode.ts +++ b/js/keyboard/KeyNode.ts @@ -109,7 +109,7 @@ export default class KeyNode extends Node { lineWidth: options.lineWidth } ); - keyIcon.boundsProperty.link( () => { + const listener = () => { // scale down the size of the keyIcon passed in if it is taller than the max height of the icon let heightScalar = 1; @@ -144,7 +144,8 @@ export default class KeyNode extends Node { backgroundShadow.setRectBounds( content.bounds.shiftedXY( options.xShadowOffset, options.yShadowOffset ) ); whiteForeground.setRectBounds( content.bounds ); - } ); + }; + keyIcon.boundsProperty.link( listener ); // children of the icon node, including the background shadow, foreground key, and content icon options.children = [ @@ -157,6 +158,8 @@ export default class KeyNode extends Node { ]; super( options ); + + this.disposeEmitter.addListener( () => keyIcon.boundsProperty.unlink( listener ) ); } }