retep998 / wio-rs

Windows things for Rust
Apache License 2.0
52 stars 14 forks source link

Should `BStr` have an `into_raw`, some way to purposefully leak? #26

Open EndilWayfare opened 3 years ago

EndilWayfare commented 3 years ago

If you're implementing a function that returns a string to foreign COM callers, you should not deallocate the returned BSTR. Since we can't pass a wio::BStr across the boundary, we'd need some way to extract the pointer and make rust forget about it. I don't see that any existing method would allow that.

c.f. ComPtr

EndilWayfare commented 3 years ago

You can, of course, explicitly spell it out in the meantime

pub fn cause_bstr_to_happen() -> winapi::shared::wtypes::BSTR {
    let bstr: BStr = "Hello World".into();
    let ptr = bstr.as_ptr();
    std::mem::forget(bstr);

    ptr
}