{"id":103358,"date":"2020-01-23T07:00:00","date_gmt":"2020-01-23T15:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103358"},"modified":"2020-01-22T17:43:38","modified_gmt":"2020-01-23T01:43:38","slug":"20200123-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200123-00\/?p=103358","title":{"rendered":"Windows Runtime delegates and object lifetime in C++\/CX, redux"},"content":{"rendered":"<p>One thing to watch out for when using delegates in C++\/CX is that <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190521-00\/?p=102505\"> invoking a delegate can raise <code>Platform::<\/code><code>Disconnected\u00adException<\/code><\/a>. If the delegate is inside a C++\/CX event, then the runtime will do the work of catching the <code>Platform::<\/code><code>Disconnected\u00adException<\/code> exception, but if you are invoking the delegate manually, then it falls to you to deal with the possibility that the delegate&#8217;s object no longer exists.<\/p>\n<pre>public delegate void MenuItemInvoked();\r\nref class CustomMenuItem\r\n{\r\npublic:\r\n  CustomMenuItem(MenuItemInvoked^ handler) :\r\n    m_handler(handler) { }\r\n\r\nprivate:\r\n  MenuItemInvoked^ m_handler;\r\n\r\n  void NotifyClientThatItemWasInvoked()\r\n  {\r\n     if (m_handler) m_handler();\r\n  }\r\n}\r\n<\/pre>\n<p>When the item is invoked, we invoke the handler, but it&#8217;s possible that the object that was supposed to handle the event has already been destroyed. In that case, the runtime will fail to resolve the weak reference to a strong reference, and it will raise the <code>Platform::<\/code><code>Disconnected\u00adException<\/code>. The above code doesn&#8217;t handle that exception, so it will crash.<\/p>\n<p>What you should do is catch the <code>Platform::<\/code><code>Disconnected\u00adException<\/code> and use that as a signal that the handler is no longer any good and shouldn&#8217;t be invoked any more.<\/p>\n<pre>  void NotifyClientThatItemWasInvoked()\r\n  {\r\n     try\r\n     {\r\n       if (m_handler) m_handler();\r\n     }\r\n     catch (Platform::DisconnectedException^)\r\n     {\r\n       \/\/ Handler is no good.\r\n       \/\/ Don't bother invoking it any more.\r\n       m_handler = nullptr;\r\n     }\r\n  }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you are the one calling the delegate, you have an extra thing to worry about.<\/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-103358","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>If you are the one calling the delegate, you have an extra thing to worry about.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103358","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=103358"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103358\/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=103358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}