@@ -18,19 +18,20 @@ extern void go_validation_interface_block_checked_bridge(void* user_data, const
1818*/
1919import "C"
2020import (
21+ "fmt"
2122 "runtime"
2223 "runtime/cgo"
2324 "unsafe"
2425)
2526
26- var _ cManagedResource = & ContextOptions {}
27+ var _ cResource = & ContextOptions {}
2728
2829// 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
3031type ContextOptions struct {
3132 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
3435}
3536
3637func NewContextOptions () (* ContextOptions , error ) {
@@ -40,7 +41,7 @@ func NewContextOptions() (*ContextOptions, error) {
4041 }
4142
4243 opts := & ContextOptions {ptr : ptr }
43- runtime .SetFinalizer (opts , (* ContextOptions ).destroy )
44+ runtime .SetFinalizer (opts , (* ContextOptions ).finalize )
4445 return opts , nil
4546}
4647
@@ -60,13 +61,10 @@ func (opts *ContextOptions) SetChainParams(chainParams *ChainParameters) {
6061func (opts * ContextOptions ) SetNotifications (callbacks * NotificationCallbacks ) error {
6162 checkReady (opts )
6263 if callbacks == nil {
63- return ErrNilNotificationCallbacks
64+ return fmt . Errorf ( "nil notification callbacks" )
6465 }
65-
66- // Clean up existing handle if present
6766 if opts .notificationHandle != 0 {
68- opts .notificationHandle .Delete ()
69- opts .notificationHandle = 0
67+ return fmt .Errorf ("notification callbacks already set" )
7068 }
7169
7270 // Create a handle for the callbacks - this prevents garbage collection
@@ -94,13 +92,10 @@ func (opts *ContextOptions) SetNotifications(callbacks *NotificationCallbacks) e
9492func (opts * ContextOptions ) SetValidationInterface (callbacks * ValidationInterfaceCallbacks ) error {
9593 checkReady (opts )
9694 if callbacks == nil {
97- return ErrNilValidationInterfaceCallbacks
95+ return fmt . Errorf ( "nil validation interface callbacks" )
9896 }
99-
100- // Clean up existing handle if present
10197 if opts .validationHandle != 0 {
102- opts .validationHandle .Delete ()
103- opts .validationHandle = 0
98+ return fmt .Errorf ("validation interface callbacks already set" )
10499 }
105100
106101 // Create a handle for the callbacks - this prevents garbage collection
@@ -118,6 +113,11 @@ func (opts *ContextOptions) SetValidationInterface(callbacks *ValidationInterfac
118113}
119114
120115func (opts * ContextOptions ) destroy () {
116+ opts .finalize ()
117+ runtime .SetFinalizer (opts , nil )
118+ }
119+
120+ func (opts * ContextOptions ) finalize () {
121121 if opts .ptr != nil {
122122 C .btck_context_options_destroy (opts .ptr )
123123 opts .ptr = nil
@@ -134,11 +134,6 @@ func (opts *ContextOptions) destroy() {
134134 }
135135}
136136
137- func (opts * ContextOptions ) Destroy () {
138- runtime .SetFinalizer (opts , nil )
139- opts .destroy ()
140- }
141-
142137func (opts * ContextOptions ) isReady () bool {
143138 return opts != nil && opts .ptr != nil
144139}
0 commit comments