Skip to content

Commit 2fe4ae1

Browse files
committed
Clean up release after cloning source structs
1 parent 729934c commit 2fe4ae1

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

arrow/src/ffi.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ bitflags! {
106106
/// ABI-compatible struct for `ArrowSchema` from C Data Interface
107107
/// See <https://arrow.apache.org/docs/format/CDataInterface.html#structure-definitions>
108108
/// This was created by bindgen
109+
///
110+
/// Although this struct supports `Clone` but it is important to make sure
111+
/// only one struct has the `release` callback after cloning. Otherwise
112+
/// double-dropping can cause memory error.
113+
///
109114
#[repr(C)]
110115
#[derive(Debug, Clone)]
111116
pub struct FFI_ArrowSchema {
@@ -116,7 +121,7 @@ pub struct FFI_ArrowSchema {
116121
n_children: i64,
117122
children: *mut *mut FFI_ArrowSchema,
118123
dictionary: *mut FFI_ArrowSchema,
119-
release: Option<unsafe extern "C" fn(arg1: *mut FFI_ArrowSchema)>,
124+
pub(crate) release: Option<unsafe extern "C" fn(arg1: *mut FFI_ArrowSchema)>,
120125
private_data: *mut c_void,
121126
}
122127

@@ -335,6 +340,11 @@ fn bit_width(data_type: &DataType, i: usize) -> Result<usize> {
335340
/// ABI-compatible struct for ArrowArray from C Data Interface
336341
/// See <https://arrow.apache.org/docs/format/CDataInterface.html#structure-definitions>
337342
/// This was created by bindgen
343+
///
344+
/// Although this struct supports `Clone` but it is important to make sure
345+
/// only one struct has the `release` callback after cloning. Otherwise
346+
/// double-dropping can cause memory error.
347+
///
338348
#[repr(C)]
339349
#[derive(Debug, Clone)]
340350
pub struct FFI_ArrowArray {
@@ -346,7 +356,7 @@ pub struct FFI_ArrowArray {
346356
pub(crate) buffers: *mut *const c_void,
347357
children: *mut *mut FFI_ArrowArray,
348358
dictionary: *mut FFI_ArrowArray,
349-
release: Option<unsafe extern "C" fn(arg1: *mut FFI_ArrowArray)>,
359+
pub(crate) release: Option<unsafe extern "C" fn(arg1: *mut FFI_ArrowArray)>,
350360
// When exported, this MUST contain everything that is owned by this array.
351361
// for example, any buffer pointed to in `buffers` must be here, as well
352362
// as the `buffers` pointer itself.
@@ -783,6 +793,11 @@ impl ArrowArray {
783793
};
784794
let ffi_array = (*array).clone();
785795
let ffi_schema = (*schema).clone();
796+
797+
// Clean up `release` of source structs
798+
(*(array as *mut FFI_ArrowArray)).release = None;
799+
(*(schema as *mut FFI_ArrowSchema)).release = None;
800+
786801
Ok(Self {
787802
array: Arc::new(ffi_array),
788803
schema: Arc::new(ffi_schema),

0 commit comments

Comments
 (0)