असल में, आप किसी ऑब्जेक्ट को सी को रिसाव करने में कामयाब रहे हैं; आप एक (शीघ्र) अस्तित्वहीन स्टैक फ्रेम के संदर्भ को रिसाव करने में कामयाब रहे हैं। : डी
यहाँ एक पूर्ण उदाहरण है कि सही ढंग से काम करना चाहिए। मैंने यह बताने के लिए उचित रूप से टिप्पणी करने की कोशिश की है कि मैं क्या कर रहा हूं और क्यों।
pub struct Dramatic(String);
// Implement a destructor just so we can see when the object is destroyed.
impl Drop for Dramatic {
fn drop(&mut self) {
println!("And lo, I, {}, meet a most terrible fate!", self.0);
}
}
pub extern "C" fn create() -> *mut Dramatic {
// We **must** heap-allocate the object! Returning a reference to a local
// will **almost certainly** break your program!
let mut obj = Box::new(Dramatic("Roger".to_string()));
// * derefs the Box into a Dramatic, the &mut re-borrows it into a regular
// reference. The constraint ensures we coerce the &mut Dramatic into
// a *mut Dramatic, which "hides" the reference from the borrow checker.
let ptr: *mut _ = &mut *obj;
// Forget discards its argument (passed by-move), without trigger its
// destructor, if it has one.
::std::mem::forget(obj);
ptr
}
pub extern "C" fn destroy(ptr: &mut *mut Dramatic) {
// First, we **must** check to see if the pointer is null.
if ptr.is_null() {
// Do nothing.
return;
}
// Now, we know the pointer is non-null, we can continue.
let obj: Box<Dramatic> = unsafe { ::std::mem::transmute(*ptr) };
// We don't *have* to do anything else; once obj goes out of scope, it will
// be dropped. I'm going to drop it explicitly, however, for clarity.
::std::mem::drop(obj);
// I am, however, going to null out the `ptr` we were passed just so the
// calling code is less likely to accidentally re-use the pointer.
*ptr = ::std::ptr::null_mut();
}
fn main() {
let mut ptr = create();
println!("ptr = {:?}", ptr);
destroy(&mut ptr);
println!("ptr = {:?}", ptr);
}
स्रोत
2015-02-02 13:12:37
'और म्यूट टी' से' * mut टी' को ट्रांसमिशन की आवश्यकता नहीं है; एक सरल 'as * mut _' करेगा। –
असमानता क्यों? 'बनाना' एक '* म्यूट ड्रामैटिक 'देता है लेकिन' नष्ट 'एक' और mut * mut ड्रामैटिक 'लेता है जबकि सी में आप दोनों के लिए समान प्रकार के' नाटकीय * 'की अपेक्षा करेंगे। –
@MatthieuM। "मैं' ptr' को बाहर निकालने जा रहा हूं, इसलिए कॉलिंग कोड गलती से सूचक का पुन: उपयोग करने की संभावना कम है "। यह एक सम्मेलन है जिसे मैंने सी में देखा है, लेकिन मुझे इसे कभी पसंद नहीं आया है। – Shepmaster