Source file
test/fixedbugs/issue32477.go
1
2
3
4
5
6
7
8
9
10
11 package main
12
13 import "runtime"
14
15 var finalized bool
16 var err string
17
18 type HeapObj [8]int64
19
20 const filler int64 = 0x123456789abcdef0
21
22 func (h *HeapObj) init() {
23 for i := 0; i < len(*h); i++ {
24 h[i] = filler
25 }
26 }
27 func (h *HeapObj) check() {
28 for i := 0; i < len(*h); i++ {
29 if h[i] != filler {
30 err = "filler overwritten"
31 }
32 }
33 }
34
35 func gc(shouldFinalize bool) {
36 runtime.GC()
37 runtime.GC()
38 runtime.GC()
39 if shouldFinalize != finalized {
40 err = "heap object finalized at the wrong time"
41 }
42 }
43
44 func main() {
45 h := new(HeapObj)
46 h.init()
47 runtime.SetFinalizer(h, func(h *HeapObj) {
48 finalized = true
49 })
50
51 gc(false)
52 g(h)
53 if err != "" {
54 panic(err)
55 }
56 }
57
58 func g(h *HeapObj) {
59 gc(false)
60 h.check()
61
62 defer func() {
63
64
65
66 gc(true)
67 recover()
68 }()
69 *(*int)(nil) = 0
70 return
71 }
72
View as plain text