renato2099 commented on code in PR #18672: URL: https://github.com/apache/datafusion/pull/18672#discussion_r2554368917
########## 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 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. + +Suppose we have a `FFI_CatalogProvider`, for example. This struct is safe to +pass across the FFI boundary, so it may be owned be either the library that +produces the underlying `CatalogProvider` or by another library that consumes +it. If we look closer at the `FFI_CatalogProvider`, it has a pointer to +some private data. That private data is only accessible on the producer's +side. If you attempt to access it on the consumer's side, you may get +segmentation faults or other bad behavior. Within that private data is the +actual `Arc<dyn CatalogProvider`. That `Arc<>` must be freed, but if the +`FFI_CatalogProvider` is only owned on the consumer's side, we have no way +to access the private data and free it. + +To account for this most structs in this crate have a `release` method that Review Comment: ```suggestion To account for this, most structs in this crate have a `release` method that ``` -- 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]
