Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Improve the two Interface pointers #125

Merged
merged 2 commits into from
Feb 18, 2020
Merged

Improve the two Interface pointers #125

merged 2 commits into from
Feb 18, 2020

Conversation

rylev
Copy link
Contributor

@rylev rylev commented Feb 18, 2020

This addresses most of the issues in #99.

InterfacePtr should only be constructed if AddRef was called on the underlying interface before the raw pointer is moved into InterfacePtr::new. The documentation of InterfacePtr::new now reflects this. It is up to the user to ensure that Release gets called when that InterfacePtr is no longer needed. This is usually done by converting InterfacePtr to an InterfaceRc through the InterfacePtr::upgrade method.

These changes make it possible to leak memory but not to violate safety rules unless some invariant is not held when calling an unsafe function.

Clone on InterfacePtr now correctly calls add_ref to ensure that the invariant of AddRef being called before InterfacePtr::new is constructed is upheld.

///
/// # Panics
///
/// Panics if `ptr` is null
pub unsafe fn new(ptr: *mut *mut <T as ComInterface>::VTable) -> InterfacePtr<T> {
InterfacePtr {
ptr: NonNull::new(ptr).expect("ptr was null"),
ptr: NonNull::new(ptr).expect("InterfacePtr's ptr was null"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recon this was the previous behavior. Is it better to return an option rather than crashing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking on this though. Feel free to merge it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be fine with that. I can make that change

@rylev rylev merged commit 64f0eb4 into master Feb 18, 2020
@rylev rylev deleted the better-interface-pointers branch February 18, 2020 14:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants