Source file
test/stackobj3.go
1
2
3
4
5
6
7
8
9 package main
10
11 import (
12 "runtime"
13 )
14
15 type HeapObj [8]int64
16
17 type StkObj struct {
18 h *HeapObj
19 }
20
21 var n int
22 var c int = -1
23
24 func gc() {
25
26 runtime.GC()
27 runtime.GC()
28 runtime.GC()
29 n++
30 }
31
32 var null StkObj
33
34 var sink *HeapObj
35
36
37 func use(p *StkObj) {
38 }
39
40
41 func f(s StkObj, b bool) {
42 var p *StkObj
43 if b {
44 p = &s
45 } else {
46 p = &null
47 }
48
49
50 use(p)
51
52 gc()
53 sink = p.h
54 gc()
55 sink = nil
56
57 gc()
58 }
59
60 func fTrue() {
61 var s StkObj
62 s.h = new(HeapObj)
63 c = -1
64 n = 0
65 runtime.SetFinalizer(s.h, func(h *HeapObj) {
66
67 c = n
68 })
69 f(s, true)
70 if c != 2 {
71 panic("bad liveness")
72 }
73 }
74
75 func fFalse() {
76 var s StkObj
77 s.h = new(HeapObj)
78 c = -1
79 n = 0
80 runtime.SetFinalizer(s.h, func(h *HeapObj) {
81
82 c = n
83 })
84 f(s, false)
85 if c != 0 {
86 panic("bad liveness")
87 }
88 }
89
90 func main() {
91 fTrue()
92 fFalse()
93 }
94
View as plain text