tasyncleak3.nim 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. discard """
  2. cmd: "nim c --gc:orc -d:useMalloc $file"
  3. output: "true"
  4. valgrind: "true"
  5. """
  6. import strutils
  7. type
  8. FutureBase* = ref object of RootObj ## Untyped future.
  9. finished: bool
  10. stackTrace: seq[StackTraceEntry] ## For debugging purposes only.
  11. proc newFuture*(): FutureBase =
  12. new(result)
  13. result.finished = false
  14. result.stackTrace = getStackTraceEntries()
  15. type
  16. PDispatcher {.acyclic.} = ref object
  17. var gDisp: PDispatcher
  18. new gDisp
  19. proc testCompletion(): FutureBase =
  20. var retFuture = newFuture()
  21. iterator testCompletionIter(): FutureBase {.closure.} =
  22. retFuture.finished = true
  23. when not defined(nobug):
  24. let disp = gDisp # even worse memory consumption this way...
  25. var nameIterVar = testCompletionIter
  26. proc testCompletionNimAsyncContinue() {.closure.} =
  27. if not nameIterVar.finished:
  28. discard nameIterVar()
  29. testCompletionNimAsyncContinue()
  30. return retFuture
  31. proc main =
  32. for i in 0..10_000:
  33. discard testCompletion()
  34. main()
  35. GC_fullCollect()
  36. echo getOccupiedMem() < 1024