summary refs log tree commit diff stats
path: root/tests/arc/tclosureiter.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2020-07-27 11:44:12 +0200
committerAndreas Rumpf <rumpf_a@web.de>2020-07-27 13:07:09 +0200
commit318f0992eaa6ddce1c306e695f95f49b319c53c7 (patch)
treed715808ac9343063e0bc237cfc459ead4dc2d81a /tests/arc/tclosureiter.nim
parent4c43915b5911b4b1d7cf35e3ba156fabde58ed84 (diff)
downloadNim-318f0992eaa6ddce1c306e695f95f49b319c53c7.tar.gz
fixes a closure iterator memory leaks, progress on #15076
Diffstat (limited to 'tests/arc/tclosureiter.nim')
-rw-r--r--tests/arc/tclosureiter.nim36
1 files changed, 36 insertions, 0 deletions
diff --git a/tests/arc/tclosureiter.nim b/tests/arc/tclosureiter.nim
new file mode 100644
index 000000000..e4a2ceac6
--- /dev/null
+++ b/tests/arc/tclosureiter.nim
@@ -0,0 +1,36 @@
+discard """
+  cmd: '''nim c -d:nimAllocStats --gc:arc $file'''
+  output: '''(allocCount: 102, deallocCount: 102)'''
+"""
+
+type
+  FutureBase = ref object
+    someData: string
+
+const
+  # Just to occupy some RAM
+  BigData = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+
+iterator mainIter(): FutureBase {.closure.} =
+  for x in 0 .. 100:
+    var internalTmpFuture = FutureBase(someData: BigData)
+    yield internalTmpFuture
+
+proc main() =
+  var nameIterVar = mainIter
+  var next = nameIterVar()
+  while not isNil(next):
+    next = nameIterVar()
+    if not isNil(next):
+      doAssert next.someData.len == 97
+    # GC_unref(next)
+    # If you uncomment the GC_ref above,
+    # the program basically uses no memory after the run.
+    # but crashes with refc, which might indicate
+    # that arc/orc simply never frees the result of "next"?
+    if finished(nameIterVar):
+      break
+
+main()
+GC_fullCollect()
+echo getAllocStats()