summary refs log tree commit diff stats
path: root/tests/arc/tasyncleak3.nim
blob: 21e963b7f22c595ec576bb1e31169988f741e771 (plain) (blame)
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
37
38
39
40
41
42
43
44
45
46
47
discard """
  cmd: "nim c --gc:orc -d:useMalloc $file"
  output: "true"
  valgrind: "true"
"""

import strutils

type
  FutureBase* = ref object of RootObj  ## Untyped future.
    finished: bool
    stackTrace: seq[StackTraceEntry] ## For debugging purposes only.

proc newFuture*(): FutureBase =
  new(result)
  result.finished = false
  result.stackTrace = getStackTraceEntries()

type
  PDispatcher {.acyclic.} = ref object

var gDisp: PDispatcher
new gDisp

proc testCompletion(): FutureBase =
  var retFuture = newFuture()

  iterator testCompletionIter(): FutureBase {.closure.} =
    retFuture.finished = true
    when not defined(nobug):
      let disp = gDisp # even worse memory consumption this way...

  var nameIterVar = testCompletionIter
  proc testCompletionNimAsyncContinue() {.closure.} =
    if not nameIterVar.finished:
      discard nameIterVar()
  testCompletionNimAsyncContinue()
  return retFuture

proc main =
  for i in 0..10_000:
    discard testCompletion()

main()

GC_fullCollect()
echo getOccupiedMem() < 1024