Skip to content

Commit

Permalink
Merge pull request scala#4280 from kanielc/SI-9095
Browse files Browse the repository at this point in the history
SI-9095 Memory leak in LinkedHasMap and LinkedHashSet
  • Loading branch information
adriaanm committed Feb 9, 2015
2 parents e8365de + f2d0231 commit 27988ca
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/library/scala/collection/mutable/LinkedHashMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class LinkedHashMap[A, B] extends AbstractMap[A, B]
override def clear() {
clearTable()
firstEntry = null
lastEntry = null
}

private def writeObject(out: java.io.ObjectOutputStream) {
Expand Down
1 change: 1 addition & 0 deletions src/library/scala/collection/mutable/LinkedHashSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class LinkedHashSet[A] extends AbstractSet[A]
override def clear() {
clearTable()
firstEntry = null
lastEntry = null
}

private def writeObject(out: java.io.ObjectOutputStream) {
Expand Down
25 changes: 25 additions & 0 deletions test/junit/scala/collection/mutable/LinkedHashMapTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package scala.collection.mutable

import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.{ Assert, Test }

import scala.collection.mutable

/* Test for SI-9095 */
@RunWith(classOf[JUnit4])
class LinkedHashMapTest {
class TestClass extends mutable.LinkedHashMap[String, Int] {
def lastItemRef = lastEntry
}

@Test
def testClear: Unit = {
val lhm = new TestClass
Seq("a" -> 8, "b" -> 9).foreach(kv => lhm.put(kv._1, kv._2))

Assert.assertNotNull(lhm.lastItemRef)
lhm.clear()
Assert.assertNull(lhm.lastItemRef)
}
}
25 changes: 25 additions & 0 deletions test/junit/scala/collection/mutable/LinkedHashSetTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package scala.collection.mutable

import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.{ Assert, Test }

import scala.collection.mutable

/* Test for SI-9095 */
@RunWith(classOf[JUnit4])
class LinkedHashSetTest {
class TestClass extends mutable.LinkedHashSet[String] {
def lastItemRef = lastEntry
}

@Test
def testClear: Unit = {
val lhs = new TestClass
Seq("a", "b").foreach(k => lhs.add(k))

Assert.assertNotNull(lhs.lastItemRef)
lhs.clear()
Assert.assertNull(lhs.lastItemRef)
}
}

0 comments on commit 27988ca

Please sign in to comment.