Skip to content

Instantly share code, notes, and snippets.

@Aatch
Last active December 16, 2015 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aatch/5440139 to your computer and use it in GitHub Desktop.
Save Aatch/5440139 to your computer and use it in GitHub Desktop.
Pointer Freeing - Does this work?
struct my_c_struct {
f1:int,
f2:int
}
type MyStruct = my_c_struct;
impl Drop for MyStruct {
fn finalize(&self) {
unsafe {
core::libc::free(ptr::addr_of(self))
cast::forget(self)
}
}
}
unsafe fn get_c_struct() -> *my_c_struct;
fn my_safe_wrapper() -> ~MyStruct {
unsafe { cast::transmute(get_c_struct()) }
}
struct Ptr<T> {
priv ptr: *T
}
#[unsafe_destructor]
impl<T> Drop for Ptr<T> {
fn finalize(&self) {
unsafe { core::libc::free(self.ptr); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment