diff --git a/waku-bindings/src/node/filter.rs b/waku-bindings/src/node/filter.rs index c2d3bc4..bd78382 100644 --- a/waku-bindings/src/node/filter.rs +++ b/waku-bindings/src/node/filter.rs @@ -16,29 +16,37 @@ pub fn waku_filter_subscribe( peer_id: PeerId, timeout: Duration, ) -> Result<()> { - let result = unsafe { - CStr::from_ptr(waku_sys::waku_filter_subscribe( - CString::new( - serde_json::to_string(filter_subscription) - .expect("FilterSubscription should always be able to be serialized"), - ) - .expect("CString should build properly from the serialized filter subscription") - .into_raw(), - CString::new(peer_id) - .expect("CString should build properly from peer id") - .into_raw(), + let filter_subscription = CString::new( + serde_json::to_string(filter_subscription) + .expect("FilterSubscription should always succeed to serialize"), + ) + .expect("FilterSubscription should always be able to be serialized"); + let peer_id = CString::new(peer_id).expect("PeerId should always be able to be serialized"); + + let result_ptr = unsafe { + let filter_subscription_ptr = filter_subscription.into_raw(); + let peer_id_ptr = peer_id.into_raw(); + let result_ptr = waku_sys::waku_filter_subscribe( + filter_subscription_ptr, + peer_id_ptr, timeout .as_millis() .try_into() .expect("Duration as milliseconds should fit in a i32"), - )) - } - .to_str() - .expect("Response should always succeed to load to a &str"); + ); + drop(CString::from_raw(filter_subscription_ptr)); + drop(CString::from_raw(peer_id_ptr)); + result_ptr + }; + let result = unsafe { CStr::from_ptr(result_ptr) } + .to_str() + .expect("Response should always succeed to load to a &str"); let response: JsonResponse = serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - + unsafe { + waku_sys::waku_utils_free(result_ptr); + } Result::from(response).map(|_| ()) } @@ -48,25 +56,29 @@ pub fn waku_filter_unsubscribe( filter_subscription: &FilterSubscription, timeout: Duration, ) -> Result<()> { - let result = unsafe { - CStr::from_ptr(waku_sys::waku_filter_unsubscribe( - CString::new( - serde_json::to_string(filter_subscription) - .expect("FilterSubscription should always be able to be serialized"), - ) - .expect("CString should build properly from the serialized filter subscription") - .into_raw(), + let filter_subscription = CString::new( + serde_json::to_string(filter_subscription) + .expect("FilterSubscription should always succeed to serialize"), + ) + .expect("CString should build properly from the serialized filter subscription"); + let result_ptr = unsafe { + let filter_subscription_ptr = filter_subscription.into_raw(); + let res = waku_sys::waku_filter_unsubscribe( + filter_subscription_ptr, timeout .as_millis() .try_into() .expect("Duration as milliseconds should fit in a i32"), - )) - } - .to_str() - .expect("Response should always succeed to load to a &str"); + ); + drop(CString::from_raw(filter_subscription_ptr)); + res + }; + let result = unsafe { CStr::from_ptr(result_ptr) } + .to_str() + .expect("Response should always succeed to load to a &str"); let response: JsonResponse = serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - + unsafe { waku_sys::waku_utils_free(result_ptr) }; Result::from(response).map(|_| ()) }