You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
two-phase initialization support to prevent double-destruction on handing 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
Copy file name to clipboardExpand all lines: nuget/readme.md
+35Lines changed: 35 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,6 +77,41 @@ To customize common C++/WinRT project properties:
77
77
* expand the Common Properties item
78
78
* select the C++/WinRT property page
79
79
80
+
## InitializeComponent
81
+
82
+
In older versions of C++/WinRT, Xaml objects called InitializeComponent from constructors. This can lead to memory corruption if InitializeComponent throws an exception.
83
+
84
+
```cpp
85
+
voidMainPage::MainPage()
86
+
{
87
+
// This pattern should no longer be used
88
+
InitializeComponent();
89
+
}
90
+
```
91
+
92
+
C++/WinRT now calls InitializeComponent automatically and safely, after object construction. Explicit calls to InitializeComponent from constructors in existing code should now be removed. Multiple calls to InitializeComponent are idempotent.
93
+
94
+
If a Xaml object needs to access a Xaml property during initialization, it should override InitializeComponent:
95
+
96
+
```cpp
97
+
voidMainPage::InitializeComponent()
98
+
{
99
+
// Call base InitializeComponent() to register with the Xaml runtime
100
+
MainPageT::InitializeComponent();
101
+
// Can now access Xaml properties
102
+
MyButton().Content(box_value(L"Click"));
103
+
}
104
+
```
105
+
106
+
A non-Xaml object can also participate in two-phase construction by defining an InitializeComponent method.
107
+
108
+
```cpp
109
+
voidMyComponent::InitializeComponent()
110
+
{
111
+
// Execute initialization logic that may throw
112
+
}
113
+
```
114
+
80
115
## Troubleshooting
81
116
82
117
The msbuild verbosity level maps to msbuild message importance as follows:
0 commit comments