diff options
-rw-r--r-- | compiler/sempass2.nim | 2 | ||||
-rw-r--r-- | tests/destructor/tarc2.nim | 29 |
2 files changed, 31 insertions, 0 deletions
diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim index 00afb737d..411617acd 100644 --- a/compiler/sempass2.nim +++ b/compiler/sempass2.nim @@ -692,6 +692,8 @@ proc track(tracked: PEffects, n: PNode) = case n.kind of nkSym: useVar(tracked, n) + if n.sym.typ != nil and tfHasAsgn in n.sym.typ.flags: + tracked.owner.flags.incl sfInjectDestructors of nkRaiseStmt: if n[0].kind != nkEmpty: n.sons[0].info = n.info diff --git a/tests/destructor/tarc2.nim b/tests/destructor/tarc2.nim new file mode 100644 index 000000000..56dbfe929 --- /dev/null +++ b/tests/destructor/tarc2.nim @@ -0,0 +1,29 @@ +discard """ + output: '''leak: true''' + cmd: '''nim c --gc:arc $file''' +""" + +type + T = ref object + s: seq[T] + data: string + +proc create(): T = T(s: @[], data: "abc") + +proc addX(x: T; data: string) = + x.data = data + +proc addX(x: T; child: T) = + x.s.add child + +proc main(rootName: string) = + var root = create() + root.data = rootName + # this implies we do the refcounting wrong. We should leak memory here + # and not create a destruction cycle: + root.addX root + +let mem = getOccupiedMem() +main("yeah") +# since we created a retain cycle, we MUST leak memory here: +echo "leak: ", getOccupiedMem() - mem > 0 |