hu6360567 edited a comment on issue #994: URL: https://github.com/apache/arrow-rs/issues/994#issuecomment-985254643
In `pyarrow.rs`, I found some snippet for import/export with FFI. https://github.com/apache/arrow-rs/blob/e9be49d962560ce5b87544a2933d8b207322cf60/arrow/src/pyarrow.rs#L110-L149 The desired workflow of importing array from FFI: 1. prepare ArrowArray and leak both pointers to FFI 2. write C Data Interface into both pointers 3. Import from both pointers, allocated in first step For safety notice of ArrowArray, https://github.com/apache/arrow-rs/blob/e9be49d962560ce5b87544a2933d8b207322cf60/arrow/src/ffi.rs#L638-L643 But, for the workflow of exporting to FFI, I didn't find when to release pointers created during `into_raw`. https://github.com/apache/arrow-rs/blob/e9be49d962560ce5b87544a2933d8b207322cf60/arrow/src/pyarrow.rs#L136 C++ `arrow::ImportArray` moves the payload of pointer, but cannot release the pointer, since it is allocated by `Arc` in rust. Does it lead to a memory leak? My proposal: ```rust pub type Result<T> = std::result::Result<T, error::Error>; pub(crate) fn export_array(array: ArrowArray, content: *mut FFI_ArrowArray, schema: *mut FFI_ArrowSchema) -> Result<()> { if content.is_null() { Err(ArrowError::MemoryError("content is null".to_string()))? } if schema.is_null() { Err(ArrowError::MemoryError("schema is null".to_string()))? } let (content_ptr, schema_ptr) = ArrowArray::into_raw(array); // swap content/content_ptr, schema/schema_ptr, like C++ std::unique_ptr unsafe { content.swap(content_ptr as *mut FFI_ArrowArray); schema.swap(schema_ptr as *mut FFI_ArrowSchema); } // release content_ptr/schema_ptr unsafe { let _ = ArrowArray::try_from_raw(content_ptr, schema_ptr); } Ok(()) } pub(crate) fn import_array(content: *const FFI_ArrowArray, schema: *const FFI_ArrowSchema) -> Result<ArrowArray> { let empty_array = unsafe { ArrowArray::empty() }; let (content_ptr, schema_ptr) = ArrowArray::into_raw(empty_array); unsafe { content.copy_to(content_ptr as *mut FFI_ArrowArray, 1); schema.copy_to(schema_ptr as *mut FFI_ArrowSchema, 1); } unsafe { Ok(ArrowArray::try_from_raw(content_ptr, schema_ptr)?) } } ``` -- 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]
