-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
two-phase initialization support to prevent double-destruction on han…
…ding out this pointer in ctor (#1130) * two-phase initialization support to prevent double-destruction on handing out this pointer in ctor * PR feedback - primarily to hide/downplay the need to support Xaml with two-phase init * should Release on exception * remove unnecessary test case * PR feedback * use smart pointer instead of raw delete
- Loading branch information
Showing
13 changed files
with
202 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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