-
Notifications
You must be signed in to change notification settings - Fork 264
two-phase initialization support to prevent double-destruction on handing out this pointer in ctor #1130
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
Merged
Merged
two-phase initialization support to prevent double-destruction on handing out this pointer in ctor #1130
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ef7c534
two-phase initialization support to prevent double-destruction on han…
Scottj1s 2e0656b
PR feedback - primarily to hide/downplay the need to support Xaml wit…
Scottj1s 6f2b56e
should Release on exception
Scottj1s 8a52919
remove unnecessary test case
Scottj1s f8d0247
PR feedback
Scottj1s ed7fd74
use smart pointer instead of raw delete
Scottj1s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| #include "pch.h" | ||
|
|
||
| using namespace winrt; | ||
| using namespace Windows::Foundation; | ||
|
|
||
| namespace | ||
| { | ||
| class some_exception : public std::exception | ||
| { | ||
| public: | ||
| some_exception() noexcept | ||
| : exception("some_exception", 1) | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| template<typename D> | ||
| struct InitializeT : implements<D, IStringable> | ||
| { | ||
| bool& m_initialize_called; | ||
|
|
||
| InitializeT(bool& initialize_called) : m_initialize_called(initialize_called) | ||
| { | ||
| } | ||
|
|
||
| ~InitializeT() | ||
| { | ||
| } | ||
|
|
||
| void InitializeComponent() | ||
| { | ||
| m_initialize_called = true; | ||
| throw some_exception(); | ||
| } | ||
|
|
||
| hstring ToString() | ||
| { | ||
| return {}; | ||
| } | ||
| }; | ||
|
|
||
| struct Initialize : InitializeT<Initialize> | ||
| { | ||
| Initialize(bool& initialize_called) : InitializeT(initialize_called) | ||
| { | ||
| } | ||
| }; | ||
|
|
||
| struct ThrowingDerived : InitializeT<ThrowingDerived> | ||
| { | ||
| ThrowingDerived(bool& initialize_called) : InitializeT(initialize_called) | ||
| { | ||
| throw some_exception(); | ||
| } | ||
| }; | ||
|
|
||
| struct OverriddenInitialize : InitializeT<OverriddenInitialize> | ||
| { | ||
| OverriddenInitialize(bool& initialize_called) : InitializeT(initialize_called) | ||
| { | ||
| } | ||
|
|
||
| void InitializeComponent() | ||
| { | ||
| m_initialize_called = true; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| TEST_CASE("initialize") | ||
| { | ||
| // Ensure that failure to initialize is failure to instantiate, with no side effects | ||
| { | ||
| bool initialize_called{}; | ||
| bool exception_caught{}; | ||
| try | ||
| { | ||
| make<Initialize>(initialize_called); | ||
| } | ||
| catch (some_exception const&) | ||
| { | ||
| exception_caught = true; | ||
| } | ||
| REQUIRE(initialize_called); | ||
| REQUIRE(exception_caught); | ||
| } | ||
|
|
||
| // Ensure that base is never initialized if exception thrown from derived/base constructor | ||
| { | ||
| bool initialize_called{}; | ||
| bool exception_caught{}; | ||
| try | ||
| { | ||
| make<ThrowingDerived>(initialize_called); | ||
| } | ||
| catch (some_exception const&) | ||
| { | ||
| exception_caught = true; | ||
| } | ||
| REQUIRE(!initialize_called); | ||
| REQUIRE(exception_caught); | ||
| } | ||
|
|
||
| // Support for overriding initialization for post-processing (e.g., accessing Xaml properties) | ||
| { | ||
| bool initialize_called{}; | ||
| bool exception_caught{}; | ||
| try | ||
| { | ||
| make<OverriddenInitialize>(initialize_called); | ||
| } | ||
| catch (some_exception const&) | ||
| { | ||
| exception_caught = true; | ||
| } | ||
| REQUIRE(initialize_called); | ||
| REQUIRE(!exception_caught); | ||
| } | ||
| } |
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
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
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
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
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
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
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.