C++/WinRT deferred destruction and safe QI during destruction#108
Merged
Conversation
DefaultRyan
reviewed
Dec 13, 2018
| { | ||
| // It's safe to QI/AddRef/Release inside destructor. | ||
| IStringable s; | ||
| check_hresult(QueryInterface(guid_of<IStringable>(), put_abi(s))); |
Member
There was a problem hiding this comment.
Some test cases involving IWeakReferenceSource and IWeakReference would be more valuable than IStringable.
Contributor
Author
There was a problem hiding this comment.
Well IStringable proves that QI et al works, but agreed that adding test for IWeakReferenceSource is important to show that Resolve does not work. 😉
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Xaml apps can get themselves into knots because they need to perform a QI in a destructor to call some cleanup implementation up or down the hierarchy. This call involves a QI after the object's reference count has already reached zero. This update adds support for debouncing the reference count, ensuring that once it reaches zero it can never be resurrected but still allows QI for temporaries required during destruction. This all seems rather messy but is unavoidable in certain Xaml apps/controls and C++/WinRT should be resilient to this. The downside of debouncing is that it makes AddRef and Release a bit more expensive. The up side is that C++/WinRT is hardened and far more resilient to such abuses.
Destruction may be deferred by providing a static final_release function and moving ownership of the unique_ptr to some other context.
In the example below once the MainPage is released (for the final time), final_release is called, which spends 5s waiting (on the thread pool) then resumes using the page’s Dispatcher (which requires QI/AddRef/Release to work). It then clears the unique_ptr, which causes the MainPage destructor to actually get called. Even here DataContext is called, which requires a QI for IFrameworkElement.
Obviously you don’t have to implement your final_release as a coroutine but that does work and makes it very simple to move destruction to a different thread.