After asking how I should go about freeing memory across the FFI boundary, someone on the Rust reddit suggested that rather than wrapping my structs in a Box
, I could use Vec::from_raw_parts
to construct a vector from the following struct, and that this could be safely dropped:
#[repr(C)]
pub struct Array {
data: *const c_void,
len: libc::size_t,
}
However, from_raw_parts
seems to require *mut _
data, so I'm not sure how to proceed…
The very short answer is
self.data as *mut u8
. But, let's talk more details...First, words of warning:
Do not use
Vec::from_raw_parts
unless the pointer came from aVec
originally. There is no guarantee that an arbitrary pointer will be compatible with aVec
and you are likely to create giant holes in your program if you proceed.Do not free a pointer that you don't own. Doing so leads to double frees, which will blow other large holes in your program.
You need to know the capacity of the vector before you can reconstruct it. Your example struct only contains a
len
. This is only acceptable if thelen
andcapacity
are equal.Now, let's see if I can follow my own rules...
Note that we don't have to do any explicit dropping of the
Vec
because the normalDrop
implementation ofVec
comes into play. We just have to make sure that we construct aVec
properly.