Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: memory leak on TlTonClient::receive #13

Merged
merged 5 commits into from
Jun 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/tl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,20 @@ impl TlTonClient {
}

pub fn receive(&self, timeout: f64) -> Option<(Result<TonResult>, Option<String>)> {
unsafe {
let c_str = tonlib_client_json_receive(self.ptr, timeout);
if c_str.is_null() {
None
let c_str = unsafe { tonlib_client_json_receive(self.ptr, timeout) };
if c_str.is_null() {
None
} else {
let c_str_slice = unsafe { CStr::from_ptr(c_str) };
if let Ok(c_str_str) = c_str_slice.to_str() {
trace!("{} receive: {}", self.tag, c_str_str);
} else {
trace!(
"{} receive: {}",
self.tag,
CStr::from_ptr(c_str)
.to_str()
.unwrap_or("<Error decoding string as UTF-8>")
);
let (result, extra) = deserialize_result_extra(c_str);
Some((result, extra))
trace!("{} receive: <Error decoding string as UTF-8>", self.tag);
}
let c_str_bytes = c_str_slice.to_bytes();
let (result, extra) =
unsafe { deserialize_result_extra(c_str_bytes.as_ptr() as *const i8) };
Some((result, extra))
}
}

Expand All @@ -101,7 +100,12 @@ impl TlTonClient {

impl Drop for TlTonClient {
fn drop(&mut self) {
unsafe { tonlib_client_json_destroy(self.ptr) }
unsafe {
if !self.ptr.is_null() {
tonlib_client_json_destroy(self.ptr);
self.ptr = std::ptr::null_mut();
}
}
}
}

Expand Down