{"id":112568,"date":"2026-07-28T07:00:00","date_gmt":"2026-07-28T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=112568"},"modified":"2026-07-28T21:56:40","modified_gmt":"2026-07-29T04:56:40","slug":"20260728-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260728-00\/?p=112568","title":{"rendered":"Making an agile version of a Windows Runtime delegate in C++\/WinRT, part 7"},"content":{"rendered":"<p>Last time, we <a title=\"Making an agile version of a Windows Runtime delegate in C++\/WinRT, part 6\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260727-00\/?p=112566\"> fixed the problem of creating a <code>unique_ptr<\/code> whose deleter&#8217;s constructor was might throw an exception<\/a>. But we&#8217;re not out of the woods yet.<\/p>\n<p>Let&#8217;s take another look at what we have:<\/p>\n<pre>    if (d.try_as&lt;::INoMarshal&gt;()) {\r\n        void* p;\r\n        if constexpr (std::is_reference_v&lt;Delegate&gt;) {\r\n            p = winrt::detach_abi(d);\r\n        } else {\r\n            winrt::copy_to_abi(d, p);\r\n        }\r\n        return\r\n            [p = std::unique_ptr&lt;void, in_context_deleter&gt;(p, {}),\r\n            token = get_context_token()](auto&amp;&amp;...args) {\r\n                if (token == get_context_token()) {\r\n                    std::remove_reference_t&lt;Delegate&gt; d;\r\n                    winrt::copy_from_abi(d, p.get());\r\n                    d(std::forward&lt;decltype(args)&gt;(args)...);\r\n                } else {\r\n                    throw winrt::hresult_error(CO_E_NOT_SUPPORTED);\r\n                }\r\n            };\r\n    }\r\n<\/pre>\n<p>We had originally broken the rule that the <code>unique_ptr(p)<\/code> constructor requires that the deleter&#8217;s default constructor not throw an exception. We fixed it by constructing the deleter explicitly as a parameter, so that the <code>unique_ptr<\/code> constructor can move it into the stored deleter without an exception.<\/p>\n<p>But wait, if an exception occurs in construction of the <code>in_<wbr \/>context_<wbr \/>deleter<\/code>, the raw pointer we created in the previous block will be leaked. It owns a reference count but doesn&#8217;t clean up in the case of an exception.<\/p>\n<p>We can fix this by creating the deleter first.<\/p>\n<pre>    if (d.try_as&lt;::INoMarshal&gt;()) {\r\n        <span style=\"border: solid 1px currentcolor;\">in_context_deleter del;<\/span>\r\n        void* p;\r\n        if constexpr (std::is_reference_v&lt;Delegate&gt;) {\r\n            p = winrt::detach_abi(d);\r\n        } else {\r\n            winrt::copy_to_abi(d, p);\r\n        }\r\n        return\r\n            [p = std::unique_ptr&lt;void, in_context_deleter&gt;(p, <span style=\"border: solid 1px currentcolor;\">std::move(del)<\/span>),\r\n            token = get_context_token()](auto&amp;&amp;...args) {\r\n                if (token == get_context_token()) {\r\n                    std::remove_reference_t&lt;Delegate&gt; d;\r\n                    winrt::copy_from_abi(d, p.get());\r\n                    d(std::forward&lt;decltype(args)&gt;(args)...);\r\n                } else {\r\n                    throw winrt::hresult_error(CO_E_NOT_SUPPORTED);\r\n                }\r\n            };\r\n    }\r\n<\/pre>\n<p>If there is an exception constructing the custom deleter, it happens before we initialze the raw pointer, so there is no leak of the reference owned by that raw pointer.<\/p>\n<p>Okay, so are we done now?<\/p>\n<p>Nope.<\/p>\n<p>More next time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Further explorations into exception safety.<\/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-112568","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Further explorations into exception safety.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112568","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=112568"}],"version-history":[{"count":1,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112568\/revisions"}],"predecessor-version":[{"id":112569,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112568\/revisions\/112569"}],"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=112568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=112568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=112568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}