forked from nim-lang/Nim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topt_cursor2.nim
76 lines (56 loc) · 1.49 KB
/
topt_cursor2.nim
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
discard """
cmd: '''nim c --gc:arc $file'''
output: '''emptyemptyempty
inner destroy
'''
"""
# bug #15039
import lists
type
Token = ref object of RootObj
children: DoublyLinkedList[Token]
Paragraph = ref object of Token
method `$`(token: Token): string {.base.} =
result = "empty"
method `$`(token: Paragraph): string =
if token.children.head == nil:
result = ""
else:
for c in token.children:
result.add $c
proc parseLeafBlockInlines(token: Token) =
token.children.append(Token())
token.children.append(Token()) # <-- this one AAA
var emNode = newDoublyLinkedNode(Token())
var i = 0
var it = token.children.head
while it != nil:
var nxt = it.next # this is not a cursor, it takes over ownership.
var childNode = it
if i == 0:
childNode.next = emNode # frees the object allocated in line 29 marked with AAA
elif i == 1:
emNode.next = childNode #
it = nxt # incref on freed data, 'nxt' is freed
inc i
proc parse() =
var token = Token()
token.children.append Paragraph()
parseLeafBlockInlines(token.children.head.value)
for children in token.children:
echo children
parse()
#------------------------------------------------------------------------------
# issue #15629
type inner = object
type outer = ref inner
proc `=destroy`(b: var inner) =
echo "inner destroy"
proc newOuter(): outer =
new(result)
type holder = object
contents: outer
proc main() =
var t: holder
t.contents = newOuter()
main()