Closed
Description
This code will cause a memory leak if compile with --gc:arc.
Example
type
Foo = ref object
data: int
parent: Foo
proc `=destroy`(self: var type(Foo()[])) =
echo "destroy foo ", self.data
for i in self.fields: i.reset
proc getParent(self: Foo): Foo = self.parent
var foo1 = Foo(data: 1)
var foo2 = Foo(data: 2, parent: foo1)
foo2.getParent.data = 1
Current Output
destroy foo 2
Expected Output
destroy foo 2
destroy foo 1
Possible Solution 1
proc getParent(self: Foo): lent Foo = self.parent
Possible Solution 2
var foo = foo2.getParent
foo.data = 1
Additional Information
$ nim -v
Nim Compiler Version 1.1.1 [Windows: amd64]
Compiled at 2020-03-16
Copyright (c) 2006-2019 by Andreas Rumpf