-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |