summary refs log tree commit diff stats
path: root/tests/destructor/tgcleak4.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/destructor/tgcleak4.nim')
-rw-r--r--tests/destructor/tgcleak4.nim47
1 files changed, 47 insertions, 0 deletions
diff --git a/tests/destructor/tgcleak4.nim b/tests/destructor/tgcleak4.nim
new file mode 100644
index 000000000..4299c8841
--- /dev/null
+++ b/tests/destructor/tgcleak4.nim
@@ -0,0 +1,47 @@
+discard """
+  outputsub: "no leak: "
+  cmd: "nim c --gc:arc $file"
+"""
+# bug #12758
+type
+  TExpr {.inheritable.} = object ## abstract base class for an expression
+  PLiteral = ref TLiteral
+  TLiteral = object of TExpr
+    x: int
+    op1: string
+  TPlusExpr = object of TExpr
+    a, b: ref TExpr
+    op2: string
+
+method eval(e: ref TExpr): int {.base.} =
+  # override this base method
+  quit "to override!"
+
+method eval(e: ref TLiteral): int = return e.x
+
+method eval(e: ref TPlusExpr): int =
+  # watch out: relies on dynamic binding
+  return eval(e.a) + eval(e.b)
+
+proc newLit(x: int): ref TLiteral =
+  new(result)
+  result.x = x
+  result.op1 = $getOccupiedMem()
+
+proc newPlus(a, b: ref TExpr): ref TPlusExpr =
+  new(result)
+  result.a = a
+  result.b = b
+  result.op2 = $getOccupiedMem()
+
+const Limit = when compileOption("gc", "markAndSweep") or compileOption("gc", "boehm"): 5*1024*1024 else: 500_000
+
+for i in 0..100_000:
+  var s: array[0..11, ref TExpr]
+  for j in 0..high(s):
+    s[j] = newPlus(newPlus(newLit(j), newLit(2)), newLit(4))
+    if eval(s[j]) != j+6:
+      quit "error: wrong result"
+  if getOccupiedMem() > Limit: quit("still a leak!")
+
+echo "no leak: ", getOccupiedMem()
-0800 committer Kartik Agaram <vc@akkartik.com> 2018-11-30 16:54:01 -0800 4809 - subx: html with 5 colors for comments' href='/akkartik/mu/commit/html/subx/examples/ex5.subx.html?h=hlt&id=14a380525ed2df295463b1906b718cb26871df3b'>14a38052 ^
ac07e589 ^
60338448 ^
695f9bf8 ^
60338448 ^
e99038ea ^
ac07e589 ^
bb2b6ba7 ^















695f9bf8 ^
bb2b6ba7 ^















c9bda4d1 ^
bb2b6ba7 ^
14a38052 ^
c56d803c ^
60338448 ^
ee0e67b9 ^

bb2b6ba7 ^
a0d3cac4 ^
14a38052 ^


bb2b6ba7 ^
e99038ea ^


33352536 ^
e99038ea ^
a0d3cac4 ^
e99038ea ^
33352536 ^
e99038ea ^
33352536 ^
e99038ea ^
33352536 ^
e99038ea ^
a0d3cac4 ^
















bb2b6ba7 ^



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102