I'm passing a Boxed sliced over FFI to a C++ method that the destructor delete[] the ptr. Is this safe to do? Thanks!
let mut b = data.into_boxed_slice();
let ptr = b.as_mut_ptr();
let len = b.len();
std::mem::forget(b);
new_data(ptr, len as i32);
pub fn new_data(arg1: *const u8, arg2: ::std::os::raw::c_int)
~Data() {
delete[] ptr
}
No, when an allocation is made by rust code, it must be deallocated by rust code. It is possible to set a global allocator for rust code, which is not equal to the C allocator. Even if it was, C++ says that only data allocated with new can be deallocated with delete. It is not allowed to mix malloc/free, new/delete, new[]/delete[] and Box::new/std::mem::drop.