You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/03/16 03:58:00 UTC

[GitHub] [arrow-rs] sunchao commented on a change in pull request #1449: Remove Clone and copy source structs internally

sunchao commented on a change in pull request #1449:
URL: https://github.com/apache/arrow-rs/pull/1449#discussion_r827605292



##########
File path: arrow/src/ffi.rs
##########
@@ -781,11 +781,19 @@ impl ArrowArray {
                     .to_string(),
             ));
         };
-        let ffi_array = (*array).clone();
-        let ffi_schema = (*schema).clone();
+
+        let array_mut = array as *mut FFI_ArrowArray;
+        let schema_mut = schema as *mut FFI_ArrowSchema;
+
+        let array_data = std::ptr::replace(array_mut, FFI_ArrowArray::empty());

Review comment:
       Actually after thinking more on this, it seems this won't address the original problem neither. It basically just calls `drop` on `FFI_ArrowArray` (which is empty), but doesn't free the memory pointed by `array` and `schema`.
   
   ```
     +-------+
     | array |
     +-------+             +----------------------------+
        |                  |                            |
        +----------------->|      FFI_ArrowArray        | <- memory leaked
                           |                            |
                           +----------------------------+
   ```
   
   For instance, if `array` and `schema` are from `Arc::into_raw`, then the memory allocated for the `Arc` will become dangling after this, and thus memory leak.
   
   I'm thinking whether we'll need two APIs, one where we are able to take the ownership of the memory allocated for the `array` and `schema` (e.g., exported by `Arc::into_raw` from Rust itself), and one where we cannot take the ownership (e.g., memory was allocated by other languages such as Java), and thus requires the exporter to free the memory by itself later. 
   
   For the latter, we can clone the content for `FFI_ArrowArray` and `FFI_ArrowSchema`, and set the content of the original `array` and `schema` to be `FFI_ArrowArray::empty()` and `FFI_ArrowSchema::empty()` so that the exporter can just safely free the memory later.
   
   




-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org