Skip to content

Commit

Permalink
fix: Memory leak 🙄
Browse files Browse the repository at this point in the history
  • Loading branch information
benaclejames committed Nov 20, 2023
1 parent de926b5 commit 2ce276a
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::ffi::{c_char, c_uchar, CStr, CString};
use std::mem::forget;
use std::ptr::null;
use std::slice;
use std::time::{SystemTime, UNIX_EPOCH};

Expand Down Expand Up @@ -199,17 +201,38 @@ fn parse(buf: &[u8], index: &mut usize) -> Result<OscMessage, ParserError> {

// Import a byte array from C# and parse it
#[no_mangle]
pub extern "C" fn parse_osc(buf: *const c_uchar, len: usize, index: &mut usize, msg: &mut OscMessage) -> bool {
pub extern "C" fn parse_osc(buf: *const c_uchar, len: usize, index: &mut usize) -> *const OscMessage {
let buf = unsafe { slice::from_raw_parts(buf, len) };
match parse(buf, index) {
Ok(parsed_msg) => {
*msg = parsed_msg; // update the provided OscMessage with the parsed message
true
let boxed_message = Box::new(parsed_msg);
let raw_ptr: *const OscMessage = Box::into_raw(boxed_message);
forget(raw_ptr);

Check warning on line 210 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite

calls to `std::mem::forget` with a value that implements `Copy` does nothing
raw_ptr
}
Err(_) => false,
Err(_) => null(),
}
}

#[no_mangle]
pub unsafe extern "C" fn free_osc_message(ptr: *const OscMessage) {
// Here we convert the ptr back to an osc message, therefore informing GC of it's existence once again
if ptr.is_null() {
return;
}

let message = Box::from_raw(ptr as *mut OscMessage);
for value in message.value.iter() {
if value.string != null() {
let value_str = CString::from_raw(value.string as *mut c_char);
drop(value_str);
}
}
let address = CString::from_raw(message.address as *mut c_char);
drop(address);
drop(message);
}

fn write_address(buf: &mut [u8], ix: &mut usize, address: &str) {
let address_bytes = address.as_bytes();
buf[*ix..*ix + address_bytes.len()].copy_from_slice(address_bytes);
Expand Down

0 comments on commit 2ce276a

Please sign in to comment.