File tree 1 file changed +6
-5
lines changed 1 file changed +6
-5
lines changed Original file line number Diff line number Diff line change @@ -10,22 +10,23 @@ type HandleList struct {
10
10
sync.RWMutex
11
11
// stores the Go pointers
12
12
handles []interface {}
13
- // indicates which indices are in use
14
- set map [int ]bool
13
+ // Indicates which indices are in use, and keeps a pointer to slot int variable (the handle)
14
+ // in the Go world, so that the Go garbage collector does not free it.
15
+ set map [int ]* int
15
16
}
16
17
17
18
func NewHandleList () * HandleList {
18
19
return & HandleList {
19
20
handles : make ([]interface {}, 5 ),
20
- set : make (map [int ]bool ),
21
+ set : make (map [int ]* int ),
21
22
}
22
23
}
23
24
24
25
// findUnusedSlot finds the smallest-index empty space in our
25
26
// list. You must only run this function while holding a write lock.
26
27
func (v * HandleList ) findUnusedSlot () int {
27
28
for i := 1 ; i < len (v .handles ); i ++ {
28
- isUsed := v .set [i ]
29
+ _ , isUsed := v .set [i ]
29
30
if ! isUsed {
30
31
return i
31
32
}
@@ -47,7 +48,7 @@ func (v *HandleList) Track(pointer interface{}) unsafe.Pointer {
47
48
48
49
slot := v .findUnusedSlot ()
49
50
v .handles [slot ] = pointer
50
- v .set [slot ] = true
51
+ v .set [slot ] = & slot // Keep a pointer to slot in Go world, so it's not freed by GC.
51
52
52
53
v .Unlock ()
53
54
You can’t perform that action at this time.
0 commit comments