forked from nim-lang/Nim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtclosureiter.nim
36 lines (31 loc) · 976 Bytes
/
tclosureiter.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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()