From 4a3b1e0a59fb193991d166d78b6229221a49a549 Mon Sep 17 00:00:00 2001 From: Shawn Pearce Date: Wed, 21 Aug 2013 22:26:27 -0700 Subject: [PATCH] CopyableLabel: Fix potential leak for unshown widgets If the label is never attached to the DOM the handler was leaking the reference to the widget and its internal DOM. Defer registering for dialog events until the widget is in the DOM and a flash movie has also been created. Change-Id: I9ca815743a8e69dc6a1a29c9c8cdb5f1400f6804 --- .../gwtexpui/clippy/client/CopyableLabel.java | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/gerrit-gwtexpui/src/main/java/com/google/gwtexpui/clippy/client/CopyableLabel.java b/gerrit-gwtexpui/src/main/java/com/google/gwtexpui/clippy/client/CopyableLabel.java index 75f2e8b5e1..f948f490c6 100644 --- a/gerrit-gwtexpui/src/main/java/com/google/gwtexpui/clippy/client/CopyableLabel.java +++ b/gerrit-gwtexpui/src/main/java/com/google/gwtexpui/clippy/client/CopyableLabel.java @@ -160,19 +160,22 @@ public class CopyableLabel extends Composite implements HasText { DOM.removeChild(getElement(), swf); } DOM.appendChild(getElement(), swf = SafeHtml.parse(h)); + initHideHandler(); + } + } - if (hideHandler == null) { - hideHandler = - UserAgent.addDialogVisibleHandler(new DialogVisibleHandler() { - @Override - public void onDialogVisible(DialogVisibleEvent event) { - swf.getStyle().setVisibility( - event.isVisible() - ? Style.Visibility.HIDDEN - : Style.Visibility.VISIBLE); - } - }); - } + private void initHideHandler() { + if (hideHandler == null && swf != null && isAttached()) { + hideHandler = + UserAgent.addDialogVisibleHandler(new DialogVisibleHandler() { + @Override + public void onDialogVisible(DialogVisibleEvent event) { + swf.getStyle().setVisibility( + event.isVisible() + ? Style.Visibility.HIDDEN + : Style.Visibility.VISIBLE); + } + }); } } @@ -195,7 +198,12 @@ public class CopyableLabel extends Composite implements HasText { } @Override - public void onUnload() { + protected void onLoad() { + initHideHandler(); + } + + @Override + protected void onUnload() { if (hideHandler != null) { hideHandler.removeHandler(); hideHandler = null;