summary refs log tree commit diff stats
path: root/tests/gc/closureleak.nim
blob: 1c39f43d5966c3105f7516f17d6398525991848a (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
discard """
  outputsub: "true"
"""

from strutils import join

type
  TFoo * = object
    id: int
    fn: proc(){.closure.}
var foo_counter = 0
var alive_foos = newseq[int](0)

proc free*(some: ref TFoo) =
  #echo "Tfoo #", some.id, " freed"
  alive_foos.del alive_foos.find(some.id)
proc newFoo*(): ref TFoo =
  new result, free
  
  result.id = foo_counter
  alive_foos.add result.id
  inc foo_counter

for i in 0 .. <10:
 discard newFoo()

for i in 0 .. <10:
  let f = newFoo()
  f.fn = proc = 
    echo f.id

GC_fullcollect()
echo alive_foos.len <= 3