Free strings on store

This commit is contained in:
Daniel Sanchez Quiros 2023-02-14 13:34:17 +01:00
parent 282673c3af
commit d0d6a97897
2 changed files with 29 additions and 19 deletions

View File

@ -319,13 +319,13 @@ pub fn waku_relay_subscribe(pubsub_topic: Option<WakuPubSubTopic>) -> Result<()>
.expect("CString should build properly from pubsub topic") .expect("CString should build properly from pubsub topic")
.into_raw(); .into_raw();
let result = unsafe { let result_ptr = unsafe {
let res = waku_sys::waku_relay_subscribe(pubsub_topic_ptr); let res = waku_sys::waku_relay_subscribe(pubsub_topic_ptr);
drop(CString::from_raw(pubsub_topic_ptr)); drop(CString::from_raw(pubsub_topic_ptr));
res res
}; };
let result = unsafe { CStr::from_ptr(result) } let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str() .to_str()
.expect("&str from result should always be extracted"); .expect("&str from result should always be extracted");
@ -346,13 +346,13 @@ pub fn waku_relay_unsubscribe(pubsub_topic: Option<WakuPubSubTopic>) -> Result<(
.expect("CString should build properly from pubsub topic") .expect("CString should build properly from pubsub topic")
.into_raw(); .into_raw();
let result = unsafe { let result_ptr = unsafe {
let res = waku_sys::waku_relay_unsubscribe(pubsub_topic_ptr); let res = waku_sys::waku_relay_unsubscribe(pubsub_topic_ptr);
drop(CString::from_raw(pubsub_topic_ptr)); drop(CString::from_raw(pubsub_topic_ptr));
res res
}; };
let result = unsafe { CStr::from_ptr(result) } let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str() .to_str()
.expect("&str from result should always be extracted"); .expect("&str from result should always be extracted");

View File

@ -16,17 +16,19 @@ pub fn waku_store_query(
peer_id: &PeerId, peer_id: &PeerId,
timeout: Option<Duration>, timeout: Option<Duration>,
) -> Result<StoreResponse> { ) -> Result<StoreResponse> {
let result = unsafe { let query_ptr = CString::new(
CStr::from_ptr(waku_sys::waku_store_query( serde_json::to_string(query).expect("StoreQuery should always be able to be serialized"),
CString::new( )
serde_json::to_string(query) .expect("CString should build properly from the serialized filter subscription")
.expect("StoreQuery should always be able to be serialized"), .into_raw();
) let peer_id_ptr = CString::new(peer_id.clone())
.expect("CString should build properly from the serialized filter subscription") .expect("CString should build properly from peer id")
.into_raw(), .into_raw();
CString::new(peer_id.clone())
.expect("CString should build properly from peer id") let result_ptr = unsafe {
.into_raw(), let res = waku_sys::waku_store_query(
query_ptr,
peer_id_ptr,
timeout timeout
.map(|timeout| { .map(|timeout| {
timeout timeout
@ -35,12 +37,20 @@ pub fn waku_store_query(
.expect("Duration as milliseconds should fit in a i32") .expect("Duration as milliseconds should fit in a i32")
}) })
.unwrap_or(0), .unwrap_or(0),
)) );
} drop(CString::from_raw(query_ptr));
.to_str() drop(CString::from_raw(peer_id_ptr));
.expect("Response should always succeed to load to a &str"); res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let response: JsonResponse<StoreResponse> = let response: JsonResponse<StoreResponse> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe { waku_sys::waku_utils_free(result_ptr) };
response.into() response.into()
} }