comphead commented on code in PR #18672: URL: https://github.com/apache/datafusion/pull/18672#discussion_r2557355721
########## datafusion/ffi/README.md: ########## @@ -101,6 +101,75 @@ In this crate we have a variety of structs which closely mimic the behavior of their internal counterparts. To see detailed notes about how to use them, see the example in `FFI_TableProvider`. +## Memory Management + +One of the advantages of Rust is the ownership model, which means programmers +_usually_ do not need to worry about memory management. When interacting with +foreign code, this is not necessarily true. If you review the structures in +this crate, you will find that many of them implement the `Drop` trait and +perform a foreign call. Review Comment: I think sometimes ago I found an article https://www.ralphminderhoud.com/blog/rust-ffi-wrong-way/ that states Rust when created a pointer to an object then shouldn't deallocate it, rather delegating the ownership to the callee site, C or Python in this case ``` #[no_mangle] pub unsafe extrn "C" fn file_data_read(f: *mut FileData, path: *const libc::c_char) -> bool { // Get pointer to heap allocated `FileData` let mut file_data = Box::from_raw(f); // Build path from c-string path argument let p = PathBuf::from(CStr::from_ptr(path).to_str().unwrap_or_default()); // Read the data and provide some minimal error handling let res = file_data.read(&p).is_ok(); // Forget the memory so Rust doesn't deallocate when `file_data` is dropped std::mem::forget(file_data); res } ``` I was thinking it might be agood knowledge to anyone reading FFI documentation -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
