{"id":109286,"date":"2024-01-18T07:00:00","date_gmt":"2024-01-18T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109286"},"modified":"2024-01-18T07:37:04","modified_gmt":"2024-01-18T15:37:04","slug":"20240118-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240118-00\/?p=109286","title":{"rendered":"Implementing two-phase initialization with ATL"},"content":{"rendered":"<p>In an attempt to solve problems with <a title=\"Getting a strong reference from the this pointer too soon\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240117-00\/?p=109276\"> exceptions thrown out of constructors that hand out COM references<\/a> in your class implement with ATL, you might notice this nice extension point called <code>Final\u00adConstruct()<\/code> and use it for the second phase of two-phase construction.<\/p>\n<pre>\/\/ ATL - this code is wrong\r\nclass MyPage : public CComObjectRootEx&lt;CComMultiThreadModel&gt;,\r\n    public CComCoClass&lt;MyPage&gt;,\r\n    public IPage\r\n{\r\npublic:\r\n    DECLARE_PROTECT_FINAL_CONSTRUCT()\r\n\r\n    HRESULT FinalConstruct() try\r\n    {\r\n        Application::LoadComponent(this, blah, blah);\r\n        something_that_might_throw();\r\n        return S_OK;\r\n    }\r\n    CATCH_RETURN();\r\n\r\n    \u27e6 ... \u27e7\r\n};\r\n<\/pre>\n<p>You thought you were clever and remembered that ATL runs the constructor with <a title=\"On objects with a reference count of zero\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050929-10\/?p=34003\"> a reference count of zero<\/a>, so you deferred the operations that use COM references to the <code>Final\u00adConstruct()<\/code>, and you used <code>DECLARE_<wbr \/>PROTECT_<wbr \/>FINAL_<wbr \/>CONSTRUCT()<\/code> to ensure that <code>Final\u00adConstruct()<\/code> runs with a nonzero reference count.<\/p>\n<p>However, if you look at how <code>CComCoClass::<wbr \/>CreateInstance<\/code> uses <code>Final\u00adConstruct()<\/code>, you&#8217;ll see that it doesn&#8217;t really work for two-phase construction:<\/p>\n<pre>template&lt;class Base&gt;\r\n\/* static *\/\r\nHRESULT WINAPI CComObject&lt;Base&gt;::CreateInstance(\r\n    CComObject&lt;Base&gt;** pp) throw()\r\n{\r\n    ATLASSERT(*pp == NULL);\r\n    if (pp == NULL)\r\n        return E_POINTER;\r\n    *pp = NULL;\r\n\r\n    HRESULT hRes = E_OUTOFMEMORY;\r\n    CComObject&lt;Base&gt;* p = NULL;\r\n    ATLTRY(p = _ATL_NEW CComObject&lt;Base&gt;())\r\n    if (p != NULL)\r\n    {\r\n        p-&gt;SetVoid(NULL);\r\n        p-&gt;InternalFinalConstructAddRef();\r\n        hRes = p-&gt;FinalConstruct();\r\n        p-&gt;InternalFinalConstructRelease();\r\n        if (hRes != S_OK) {\r\n            delete p;\r\n            p = NULL;\r\n        }\r\n    }\r\n    *pp = p;\r\n    return hRes;\r\n}\r\n<\/pre>\n<p>Observe that if <code>FinalConstruct()<\/code> fails, the object is outright <code>delete<\/code>d; any <code>AddRef<\/code> that occurred during <code>Final\u00adConstruct()<\/code> won&#8217;t prevent the object&#8217;s destruction.<\/p>\n<p>You will have to implement the two-phase construction manually.<\/p>\n<pre>class MyPage : public CComObjectRootEx&lt;CComMultiThreadModel&gt;,\r\n    public CComCoClass&lt;MyPage&gt;,\r\n    public IPage\r\n{\r\npublic:\r\n    <span style=\"border: solid 1px currentcolor;\">HRESULT InitializeComponent() noexcept try<\/span>\r\n    {\r\n        Application::LoadComponent(this, blah, blah);\r\n        something_that_might_throw();\r\n        return S_OK;\r\n    }\r\n    <span style=\"border: solid 1px currentcolor;\">CATCH_RETURN();<\/span>\r\n\r\n    \u27e6 ... \u27e7\r\n};\r\n\r\nHRESULT CreateMyPage(CComObject&lt;MyPage&gt;** result)\r\n{\r\n    *result = NULL;\r\n\r\n    CComObject&lt;MyPage&gt;* page;\r\n\r\n    HRESULT hr = CComObject&lt;MyPage&gt;::CreateInstance(&amp;page);\r\n    if (FAILED(hr)) return hr;\r\n\r\n    CComPtr&lt;CComObject&lt;MyPage&gt;&gt; pageRef(page);\r\n\r\n    hr = page-&gt;InitializeComponent());\r\n    if (FAILED(hr)) return hr;\r\n\r\n    *result = pageRef.Detach();\r\n    return S_OK;\r\n}\r\n<\/pre>\n<p>The important things to note are<\/p>\n<ul>\n<li>We bump the reference count from 0 to 1 (by putting it in a <code>CComPtr<\/code>) before calling <code>Initialize\u00adComponent()<\/code>, so that the COM references we hand out have a nonzero reference count.<\/li>\n<li>We use a <code>CComPtr<\/code> so that the reference will be released automatically if <code>Initialize\u00adComponent()<\/code> throws an exception or returns a COM failure.<\/li>\n<li>The <code>CComPtr<\/code> destructor does a <code>Release()<\/code> rather than a <code>delete<\/code>, so any extra references created by <code>Initialize\u00adComponent()<\/code> are honored.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>ATL looks like it supports two-phase initialization, but it doesn&#8217;t.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-109286","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>ATL looks like it supports two-phase initialization, but it doesn&#8217;t.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=109286"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109286\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=109286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=109286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=109286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}