Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide easy way to retain-construct from a reference #20

Open
Manishearth opened this issue Aug 13, 2019 · 4 comments
Open

Provide easy way to retain-construct from a reference #20

Manishearth opened this issue Aug 13, 2019 · 4 comments

Comments

@Manishearth
Copy link

Currently ComPtr::from_raw adopts the given pointer, which means that it assumes that it has already been addreffed.

This isn't always true, though, sometimes you want to retain the refcount of the old pointer, which you're simply borrowing from to create a new reference. We should have a separate constructor for this, the current way of doing it is to mem::forget(ptr.clone()) which is clunky.

@retep998
Copy link
Owner

Something like ComPtr::clone_from_raw?

@Manishearth
Copy link
Author

Yep!

@mildcwen
Copy link

mildcwen commented Jan 20, 2022

@retep998 @Manishearth
How about manully call Release instead of impl Drop?
The main reason for i use ComPtr just want to wrap the raw pointer to call it's method..
use Deref to instead (ptr -> m() in C ),

if need change RefCount, we could call clone() or Release() explicitly

    pub unsafe fn from_fn<F>(fun: F) -> Result<ComPtr<T>, HRESULT>
    where
        T: Interface,
        F: FnOnce(*mut *mut T) -> HRESULT,
{}

// I had change the signature like this, but still not ideal.
// I think just need new()/from_raw(), to Construct,

This is common to store pointer in WndExtraMemtory, and to retrieve, as raw pointer, ComPtr::new(p),
deal something, then leave without Release...

@Manishearth
Copy link
Author

I mean yes, there are many ways to make it work, but in this case adopt/retain semantics are a common difference and I think it would be good for documentation, even, to have both as first class APIs that are well documented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants