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 2021/12/03 07:44:20 UTC

[GitHub] [arrow] hu6360567 commented on issue #11846: Crash when import/export between C++ and Rust

hu6360567 commented on issue #11846:
URL: https://github.com/apache/arrow/issues/11846#issuecomment-985286141


   I've figured out the key problem, I missed up rust/C++ mangaged FFI pointers.
   In `import_array`,  it consumes the C++ managed pointers.
   
   As https://github.com/apache/arrow-rs/issues/994#issuecomment-985254643 mentioned, when calling `arrow::ExportRecordBatch`, the output pointers should be created in rust.
   
   Fix patch as below,
   ```rust
   #[no_mangle]
   pub extern "C" fn create_array(content: *mut *const FFI_ArrowArray, schema: *mut *const FFI_ArrowSchema) {
       unsafe {
           let array = ArrowArray::empty();
           let (c, s) = ArrowArray::into_raw(array);
           *content = c;
           *schema = s;
       }
   }
   ```
   
   ```c++
   void test2() {
       auto property = generateRB();
       const ArrowArray *array = nullptr;
       const ArrowSchema *schema = nullptr;
       create_array(&array, &schema);
       printf("array@%p, schema@%p\n", array, schema);
       DEBUG_C_INTERFACE("TEST2 Before Export", array, schema);
       std::cout << property->column_data(0).use_count() << "," << property->column_data(1).use_count() << std::endl;
       arrow::ExportRecordBatch(*property, const_cast<ArrowArray *>(array), const_cast<ArrowSchema *>(schema));
       DEBUG_C_INTERFACE("TEST2 After Export", array, schema);
       std::cout << property->column_data(0).use_count() << "," << property->column_data(1).use_count() << std::endl;
   
       DEBUG_C_INTERFACE("TEST2 Before Import", array, schema);
       std::cout << property->column_data(0).use_count() << "," << property->column_data(1).use_count() << std::endl;
       import_array(array, schema);
       DEBUG_C_INTERFACE("TEST2 After Import", array, schema);
       std::cout << property->column_data(0).use_count() << "," << property->column_data(1).use_count() << std::endl;
   }
   ```


-- 
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