Skip to content

Commit d376502

Browse files
committed
free the right way
Signed-off-by: Andrew Duffy <andrew@a10y.dev>
1 parent 3b5f827 commit d376502

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

vortex-cuda/cudf-test/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
66
#![allow(clippy::unwrap_used)]
77

8+
use std::sync::LazyLock;
9+
810
use arrow_schema::DataType;
911
use arrow_schema::ffi::FFI_ArrowSchema;
1012
use futures::executor::block_on;
11-
use std::sync::LazyLock;
1213
use vortex::array::IntoArray;
1314
use vortex::array::arrays::PrimitiveArray;
1415
use vortex::array::session::ArraySession;

vortex-cuda/src/arrow/canonical.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,18 @@ async fn export_primitive(
6060
ctx: &mut CudaExecutionCtx,
6161
) -> VortexResult<ArrowArray> {
6262
unsafe extern "C" fn release(array: *mut ArrowArray) {
63-
// SAFETY: this is only safe if the caller provides a valid pointer to an `ArrowArray`.
64-
drop(unsafe { Box::from_raw(array) });
65-
}
63+
// SAFETY: this is only safe if we're dropping an ArrowArray that was created from Rust
64+
// code. This is necessary to ensure that the fields inside the CudaPrivateData
65+
// get dropped to free native/GPU memory.
66+
unsafe {
67+
let private_data_ptr =
68+
std::ptr::replace(&raw mut (*array).private_data, std::ptr::null_mut());
69+
drop(Box::from_raw(private_data_ptr.cast::<CudaPrivateData>()));
6670

71+
// update the release function to NULL to avoid any possibility of double-frees.
72+
(*array).release = None;
73+
}
74+
}
6775
let len = array.len();
6876
let PrimitiveArrayParts {
6977
buffer, validity, ..

vortex-cuda/src/arrow/mod.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,6 @@ impl ArrowArray {
103103
}
104104
}
105105

106-
impl Drop for ArrowArray {
107-
fn drop(&mut self) {
108-
// SAFETY: this is only safe if we're dropping an ArrowArray that was created from Rust
109-
// code. This is necessary to ensure that the fields inside of the CudaPrivateData
110-
// get dropped to free native/GPU memory.
111-
drop(unsafe { Box::from_raw(self.private_data.cast::<CudaPrivateData>()) })
112-
}
113-
}
114-
115106
#[expect(
116107
unused,
117108
reason = "cuda_stream and cuda_buffers need to have deferred drop"

0 commit comments

Comments
 (0)