@@ -18,19 +18,20 @@ extern void go_validation_interface_block_checked_bridge(void* user_data, const
18
18
*/
19
19
import "C"
20
20
import (
21
+ "fmt"
21
22
"runtime"
22
23
"runtime/cgo"
23
24
"unsafe"
24
25
)
25
26
26
- var _ cManagedResource = & ContextOptions {}
27
+ var _ cResource = & ContextOptions {}
27
28
28
29
// ContextOptions wraps the C btck_ContextOptions.
29
- // Once the options is set on a context, the context is responsible for its lifetime and should not be destroyed manually.
30
+ // Once the options is set on a context, the context is responsible for its lifetime; otherwise it is garbage collected
30
31
type ContextOptions struct {
31
32
ptr * C.btck_ContextOptions
32
- notificationHandle cgo.Handle // Prevents notification callbacks GC until Destroy() called
33
- validationHandle cgo.Handle // Prevents validation callbacks GC until Destroy() called
33
+ notificationHandle cgo.Handle
34
+ validationHandle cgo.Handle
34
35
}
35
36
36
37
func NewContextOptions () (* ContextOptions , error ) {
@@ -40,7 +41,7 @@ func NewContextOptions() (*ContextOptions, error) {
40
41
}
41
42
42
43
opts := & ContextOptions {ptr : ptr }
43
- runtime .SetFinalizer (opts , (* ContextOptions ).destroy )
44
+ runtime .SetFinalizer (opts , (* ContextOptions ).finalize )
44
45
return opts , nil
45
46
}
46
47
@@ -60,13 +61,10 @@ func (opts *ContextOptions) SetChainParams(chainParams *ChainParameters) {
60
61
func (opts * ContextOptions ) SetNotifications (callbacks * NotificationCallbacks ) error {
61
62
checkReady (opts )
62
63
if callbacks == nil {
63
- return ErrNilNotificationCallbacks
64
+ return fmt . Errorf ( "nil notification callbacks" )
64
65
}
65
-
66
- // Clean up existing handle if present
67
66
if opts .notificationHandle != 0 {
68
- opts .notificationHandle .Delete ()
69
- opts .notificationHandle = 0
67
+ return fmt .Errorf ("notification callbacks already set" )
70
68
}
71
69
72
70
// Create a handle for the callbacks - this prevents garbage collection
@@ -94,13 +92,10 @@ func (opts *ContextOptions) SetNotifications(callbacks *NotificationCallbacks) e
94
92
func (opts * ContextOptions ) SetValidationInterface (callbacks * ValidationInterfaceCallbacks ) error {
95
93
checkReady (opts )
96
94
if callbacks == nil {
97
- return ErrNilValidationInterfaceCallbacks
95
+ return fmt . Errorf ( "nil validation interface callbacks" )
98
96
}
99
-
100
- // Clean up existing handle if present
101
97
if opts .validationHandle != 0 {
102
- opts .validationHandle .Delete ()
103
- opts .validationHandle = 0
98
+ return fmt .Errorf ("validation interface callbacks already set" )
104
99
}
105
100
106
101
// Create a handle for the callbacks - this prevents garbage collection
@@ -118,6 +113,11 @@ func (opts *ContextOptions) SetValidationInterface(callbacks *ValidationInterfac
118
113
}
119
114
120
115
func (opts * ContextOptions ) destroy () {
116
+ opts .finalize ()
117
+ runtime .SetFinalizer (opts , nil )
118
+ }
119
+
120
+ func (opts * ContextOptions ) finalize () {
121
121
if opts .ptr != nil {
122
122
C .btck_context_options_destroy (opts .ptr )
123
123
opts .ptr = nil
@@ -134,11 +134,6 @@ func (opts *ContextOptions) destroy() {
134
134
}
135
135
}
136
136
137
- func (opts * ContextOptions ) Destroy () {
138
- runtime .SetFinalizer (opts , nil )
139
- opts .destroy ()
140
- }
141
-
142
137
func (opts * ContextOptions ) isReady () bool {
143
138
return opts != nil && opts .ptr != nil
144
139
}
0 commit comments