@@ -15,42 +15,18 @@ extern void go_notify_warning_unset_bridge(void* user_data, btck_Warning warning
1515extern void go_notify_flush_error_bridge(void* user_data, const char* message, size_t message_len); 
1616extern void go_notify_fatal_error_bridge(void* user_data, const char* message, size_t message_len); 
1717extern void go_validation_interface_block_checked_bridge(void* user_data, const btck_BlockPointer* block, const btck_BlockValidationState* state); 
18- 
19- // Wrapper function: C helper to set notifications with Go callbacks 
20- // Converts Handle ID to void* and passes to C library 
21- static inline void set_notifications_wrapper(btck_ContextOptions* opts, uintptr_t handle) { 
22-     btck_NotificationInterfaceCallbacks callbacks = { 
23-         .user_data = (void*)handle, 
24-         .block_tip = (btck_NotifyBlockTip)go_notify_block_tip_bridge, 
25-         .header_tip = (btck_NotifyHeaderTip)go_notify_header_tip_bridge, 
26-         .progress = (btck_NotifyProgress)go_notify_progress_bridge, 
27-         .warning_set = (btck_NotifyWarningSet)go_notify_warning_set_bridge, 
28-         .warning_unset = (btck_NotifyWarningUnset)go_notify_warning_unset_bridge, 
29-         .flush_error = (btck_NotifyFlushError)go_notify_flush_error_bridge, 
30-         .fatal_error = (btck_NotifyFatalError)go_notify_fatal_error_bridge, 
31-     }; 
32-     btck_context_options_set_notifications(opts, callbacks); 
33- } 
34- 
35- // Wrapper function: C helper to set validation interface with Go callbacks 
36- // Converts Handle ID to void* and passes to C library 
37- static inline void set_validation_interface_wrapper(btck_ContextOptions* opts, uintptr_t handle) { 
38-     btck_ValidationInterfaceCallbacks callbacks = { 
39-         .user_data = (void*)handle, 
40-         .block_checked = (btck_ValidationInterfaceBlockChecked)go_validation_interface_block_checked_bridge, 
41-     }; 
42-     btck_context_options_set_validation_interface(opts, callbacks); 
43- } 
4418*/ 
4519import  "C" 
4620import  (
4721	"runtime" 
4822	"runtime/cgo" 
23+ 	"unsafe" 
4924)
5025
5126var  _  cManagedResource  =  & ContextOptions {}
5227
53- // ContextOptions wraps the C btck_ContextOptions 
28+ // 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. 
5430type  ContextOptions  struct  {
5531	ptr                 * C.btck_ContextOptions 
5632	notificationHandle  cgo.Handle  // Prevents notification callbacks GC until Destroy() called 
@@ -87,15 +63,29 @@ func (opts *ContextOptions) SetNotifications(callbacks *NotificationCallbacks) e
8763		return  ErrNilNotificationCallbacks 
8864	}
8965
66+ 	// Clean up existing handle if present 
67+ 	if  opts .notificationHandle  !=  0  {
68+ 		opts .notificationHandle .Delete ()
69+ 		opts .notificationHandle  =  0 
70+ 	}
71+ 
9072	// Create a handle for the callbacks - this prevents garbage collection 
9173	// and provides a stable ID that can be passed through C code safely 
92- 	handle  :=  cgo .NewHandle (callbacks )
93- 
94- 	// Call the C wrapper function to set all notification callbacks 
95- 	C .set_notifications_wrapper (opts .ptr , C .uintptr_t (handle ))
96- 
97- 	// Store the handle to prevent GC and allow cleanup 
98- 	opts .notificationHandle  =  handle 
74+ 	opts .notificationHandle  =  cgo .NewHandle (callbacks )
75+ 
76+ 	// Create notification callbacks struct and call C library directly 
77+ 	notificationCallbacks  :=  C.btck_NotificationInterfaceCallbacks {
78+ 		user_data :         unsafe .Pointer (opts .notificationHandle ),
79+ 		user_data_destroy : nil , // Go handles memory management via cgo.Handle 
80+ 		block_tip :         C .btck_NotifyBlockTip (C .go_notify_block_tip_bridge ),
81+ 		header_tip :        C .btck_NotifyHeaderTip (C .go_notify_header_tip_bridge ),
82+ 		progress :          C .btck_NotifyProgress (C .go_notify_progress_bridge ),
83+ 		warning_set :       C .btck_NotifyWarningSet (C .go_notify_warning_set_bridge ),
84+ 		warning_unset :     C .btck_NotifyWarningUnset (C .go_notify_warning_unset_bridge ),
85+ 		flush_error :       C .btck_NotifyFlushError (C .go_notify_flush_error_bridge ),
86+ 		fatal_error :       C .btck_NotifyFatalError (C .go_notify_fatal_error_bridge ),
87+ 	}
88+ 	C .btck_context_options_set_notifications (opts .ptr , notificationCallbacks )
9989	return  nil 
10090}
10191
@@ -107,15 +97,23 @@ func (opts *ContextOptions) SetValidationInterface(callbacks *ValidationInterfac
10797		return  ErrNilValidationInterfaceCallbacks 
10898	}
10999
100+ 	// Clean up existing handle if present 
101+ 	if  opts .validationHandle  !=  0  {
102+ 		opts .validationHandle .Delete ()
103+ 		opts .validationHandle  =  0 
104+ 	}
105+ 
110106	// Create a handle for the callbacks - this prevents garbage collection 
111107	// and provides a stable ID that can be passed through C code safely 
112- 	handle   : =  cgo .NewHandle (callbacks )
108+ 	opts . validationHandle   =  cgo .NewHandle (callbacks )
113109
114- 	// Call the C wrapper function to set all validation interface callbacks 
115- 	C .set_validation_interface_wrapper (opts .ptr , C .uintptr_t (handle ))
116- 
117- 	// Store the handle to prevent GC and allow cleanup 
118- 	opts .validationHandle  =  handle 
110+ 	// Create validation callbacks struct and call C library directly 
111+ 	validationCallbacks  :=  C.btck_ValidationInterfaceCallbacks {
112+ 		user_data :         unsafe .Pointer (opts .validationHandle ),
113+ 		user_data_destroy : nil , // Go handles memory management via cgo.Handle 
114+ 		block_checked :     C .btck_ValidationInterfaceBlockChecked (C .go_validation_interface_block_checked_bridge ),
115+ 	}
116+ 	C .btck_context_options_set_validation_interface (opts .ptr , validationCallbacks )
119117	return  nil 
120118}
121119
0 commit comments