Last active
December 16, 2015 13:09
-
-
Save Aatch/5440139 to your computer and use it in GitHub Desktop.
Pointer Freeing - Does this work?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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