Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LazyList potentially memory leak? #10843

Closed
xuwei-k opened this issue Apr 24, 2018 · 1 comment
Closed

LazyList potentially memory leak? #10843

xuwei-k opened this issue Apr 24, 2018 · 1 comment

Comments

@xuwei-k
Copy link

xuwei-k commented Apr 24, 2018

Should we change like this?

--- a/src/library/scala/collection/immutable/LazyList.scala
+++ b/src/library/scala/collection/immutable/LazyList.scala
@@ -564,17 +564,17 @@ object LazyList extends LazyListFactory[LazyList] {
     protected def headDefined: Boolean = false
   }
 
-  final class Cons[A](hd: => A, tl: => LazyList[A]) extends LazyList[A] {
-    private[this] var hdEvaluated: Boolean = false
-    private[this] var tlEvaluated: Boolean = false
+  final class Cons[A](private var hd: () => A, private var tl: () => LazyList[A]) extends LazyList[A] {
     override def isEmpty: Boolean = false
     override lazy val head: A = {
-      hdEvaluated = true
-      hd
+      val result = hd()
+      hd = null
+      result
     }
     override lazy val tail: LazyList[A] = {
-      tlEvaluated = true
-      tl
+      val result = tl()
+      tl = null
+      result
     }
     def force: this.type = {
       // Use standard 2x 1x iterator trick for cycle detection ("those" is slow one)
@@ -596,8 +596,8 @@ object LazyList extends LazyListFactory[LazyList] {
       this
     }
 
-    protected def tailDefined: Boolean = tlEvaluated
-    protected def headDefined: Boolean = hdEvaluated
+    protected def tailDefined: Boolean = tl == null
+    protected def headDefined: Boolean = hd == null
   }
@SethTisue SethTisue added this to the 2.13.0-M4 milestone Apr 24, 2018
@lrytz lrytz modified the milestones: 2.13.0-M4, 2.13.0-M5 Apr 24, 2018
@szeiger szeiger modified the milestones: 2.13.0-M5, 2.13.0-RC1 Aug 8, 2018
@xuwei-k
Copy link
Author

xuwei-k commented Aug 24, 2018

@xuwei-k xuwei-k closed this as completed Aug 24, 2018
@SethTisue SethTisue modified the milestones: 2.13.0-RC1, 2.13.0-M5 Aug 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants