From: John Ahlroos Date: Thu, 21 Feb 2013 13:55:57 +0000 (+0200) Subject: Fixed memory leak in Tree #11053 X-Git-Tag: 7.0.2~39 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=834275130dce0fee314e2c5b0b092579ba3905a4;p=vaadin-framework.git Fixed memory leak in Tree #11053 Change-Id: I4c58367d4041b5e61989fca5be6b037f7f8e0c01 --- diff --git a/server/src/com/vaadin/ui/Tree.java b/server/src/com/vaadin/ui/Tree.java index 608c947d59..219b39020b 100644 --- a/server/src/com/vaadin/ui/Tree.java +++ b/server/src/com/vaadin/ui/Tree.java @@ -1670,4 +1670,21 @@ public class Tree extends AbstractSelect implements Container.Hierarchical, return itemDescriptionGenerator; } + @Override + public void containerItemSetChange( + com.vaadin.data.Container.ItemSetChangeEvent event) { + + // Ensure removed items are cleaned up from expanded list + for (Object expandedItemId : expanded) { + if (getItem(expandedItemId) == null) { + expanded.remove(expandedItemId); + if (this.expandedItemId == expandedItemId) { + this.expandedItemId = null; + } + } + } + + super.containerItemSetChange(event); + } + } diff --git a/server/tests/src/com/vaadin/tests/server/component/tree/TreeTest.java b/server/tests/src/com/vaadin/tests/server/component/tree/TreeTest.java index 16a7091947..f467a42339 100644 --- a/server/tests/src/com/vaadin/tests/server/component/tree/TreeTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/tree/TreeTest.java @@ -1,5 +1,8 @@ package com.vaadin.tests.server.component.tree; +import java.lang.reflect.Field; +import java.util.HashSet; + import junit.framework.TestCase; import com.vaadin.data.Container; @@ -71,4 +74,42 @@ public class TreeTest extends TestCase { assertTrue(Container.Hierarchical.class.isAssignableFrom(tree4 .getContainerDataSource().getClass())); } + + public void testRemoveExpandedItems() throws Exception { + tree.expandItem("parent"); + tree.expandItem("child"); + + Field expandedField = tree.getClass() + .getDeclaredField("expanded"); + Field expandedItemIdField = tree.getClass().getDeclaredField( + "expandedItemId"); + + expandedField.setAccessible(true); + expandedItemIdField.setAccessible(true); + + HashSet expanded = (HashSet) expandedField.get(tree); + Object expandedItemId = expandedItemIdField.get(tree); + + assertEquals(2, expanded.size()); + assertTrue("Contains parent", expanded.contains("parent")); + assertTrue("Contains child", expanded.contains("child")); + assertEquals("child", expandedItemId); + + tree.removeItem("parent"); + + expanded = (HashSet) expandedField.get(tree); + expandedItemId = expandedItemIdField.get(tree); + + assertEquals(1, expanded.size()); + assertTrue("Contains child", expanded.contains("child")); + assertEquals("child", expandedItemId); + + tree.removeItem("child"); + + expanded = (HashSet) expandedField.get(tree); + expandedItemId = expandedItemIdField.get(tree); + + assertEquals(0, expanded.size()); + assertNull(expandedItemId); + } }