From d0d6a978978cee49acc677dbbf2fbfd13cef35f2 Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Tue, 14 Feb 2023 13:34:17 +0100 Subject: [PATCH] Free strings on store --- waku-bindings/src/node/relay.rs | 8 +++---- waku-bindings/src/node/store.rs | 40 ++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/waku-bindings/src/node/relay.rs b/waku-bindings/src/node/relay.rs index fe8edd9..52c5d0d 100644 --- a/waku-bindings/src/node/relay.rs +++ b/waku-bindings/src/node/relay.rs @@ -319,13 +319,13 @@ pub fn waku_relay_subscribe(pubsub_topic: Option) -> Result<()> .expect("CString should build properly from pubsub topic") .into_raw(); - let result = unsafe { + let result_ptr = unsafe { let res = waku_sys::waku_relay_subscribe(pubsub_topic_ptr); drop(CString::from_raw(pubsub_topic_ptr)); res }; - let result = unsafe { CStr::from_ptr(result) } + let result = unsafe { CStr::from_ptr(result_ptr) } .to_str() .expect("&str from result should always be extracted"); @@ -346,13 +346,13 @@ pub fn waku_relay_unsubscribe(pubsub_topic: Option) -> Result<( .expect("CString should build properly from pubsub topic") .into_raw(); - let result = unsafe { + let result_ptr = unsafe { let res = waku_sys::waku_relay_unsubscribe(pubsub_topic_ptr); drop(CString::from_raw(pubsub_topic_ptr)); res }; - let result = unsafe { CStr::from_ptr(result) } + let result = unsafe { CStr::from_ptr(result_ptr) } .to_str() .expect("&str from result should always be extracted"); diff --git a/waku-bindings/src/node/store.rs b/waku-bindings/src/node/store.rs index 818b10e..76202c9 100644 --- a/waku-bindings/src/node/store.rs +++ b/waku-bindings/src/node/store.rs @@ -16,17 +16,19 @@ pub fn waku_store_query( peer_id: &PeerId, timeout: Option, ) -> Result { - let result = unsafe { - CStr::from_ptr(waku_sys::waku_store_query( - CString::new( - serde_json::to_string(query) - .expect("StoreQuery should always be able to be serialized"), - ) - .expect("CString should build properly from the serialized filter subscription") - .into_raw(), - CString::new(peer_id.clone()) - .expect("CString should build properly from peer id") - .into_raw(), + let query_ptr = CString::new( + serde_json::to_string(query).expect("StoreQuery should always be able to be serialized"), + ) + .expect("CString should build properly from the serialized filter subscription") + .into_raw(); + let peer_id_ptr = CString::new(peer_id.clone()) + .expect("CString should build properly from peer id") + .into_raw(); + + let result_ptr = unsafe { + let res = waku_sys::waku_store_query( + query_ptr, + peer_id_ptr, timeout .map(|timeout| { timeout @@ -35,12 +37,20 @@ pub fn waku_store_query( .expect("Duration as milliseconds should fit in a i32") }) .unwrap_or(0), - )) - } - .to_str() - .expect("Response should always succeed to load to a &str"); + ); + drop(CString::from_raw(query_ptr)); + drop(CString::from_raw(peer_id_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) }; + response.into() }