tclosureiter.nim 976 B

12345678910111213141516171819202122232425262728293031323334353637
  1. discard """
  2. cmd: '''nim c -d:nimAllocStats --gc:arc $file'''
  3. output: '''(allocCount: 102, deallocCount: 102)'''
  4. """
  5. type
  6. FutureBase = ref object
  7. someData: string
  8. const
  9. # Just to occupy some RAM
  10. BigData = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  11. iterator mainIter(): FutureBase {.closure.} =
  12. for x in 0 .. 100:
  13. var internalTmpFuture = FutureBase(someData: BigData)
  14. yield internalTmpFuture
  15. proc main() =
  16. var nameIterVar = mainIter
  17. var next = nameIterVar()
  18. while not isNil(next):
  19. next = nameIterVar()
  20. if not isNil(next):
  21. doAssert next.someData.len == 97
  22. # GC_unref(next)
  23. # If you uncomment the GC_ref above,
  24. # the program basically uses no memory after the run.
  25. # but crashes with refc, which might indicate
  26. # that arc/orc simply never frees the result of "next"?
  27. if finished(nameIterVar):
  28. break
  29. main()
  30. GC_fullCollect()
  31. echo getAllocStats()