{"id":103725,"date":"2020-05-04T07:00:00","date_gmt":"2020-05-04T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103725"},"modified":"2020-05-04T06:49:08","modified_gmt":"2020-05-04T13:49:08","slug":"20200504-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200504-00\/?p=103725","title":{"rendered":"Peeking inside C++\/CX weak references"},"content":{"rendered":"<p>Let&#8217;s hope you never need to do this, but if you are forced to debug code written in C++\/CX, and you have a C++\/CX weak reference, say because you <!-- backref: Peeking inside C++\/CX Windows Runtime delegates --> pulled it out of a C++\/CX delegate, and you want to know what it actually refers to, well, here goes.<\/p>\n<pre>0:003&gt; $ 64-bit version\r\n0:003&gt; dps 00000203`a8773600\r\n00000203`a8773600  00007ffd`b63d6600 wincorlib!Platform::Details::ControlBlock::`vftable'\r\n00000203`a8773608  00000006`0000006d \u21d0 reference counts\r\n00000203`a8773610  00000203`a7edb710 \u21d0 target\r\n\r\n0:003&gt; $ 32-bit version\r\n0:003&gt; dps 18773600\r\n18773600  163d6600 wincorlib!Platform::Details::ControlBlock::`vftable'\r\n18773604  00000006 \u21d0 weak reference count\r\n18773608  0000006d \u21d0 strong reference count\r\n1877360c  17edb710 \u21d0 target\r\n<\/pre>\n<p>You can find this structure in the header file <code>vccorlib.h<\/code>:<\/p>\n<pre>namespace Platform { namespace Details\r\n{\r\n    class ControlBlock sealed : public __abi_IWeakReference\r\n    {\r\n    private:\r\n        volatile long __weakRefCount;\r\n        volatile long __strongRefCount;\r\n        __abi_IUnknown* __target;\r\n        ...\r\n    }\r\n<\/pre>\n<p>In general, weak references tend to rely on a <i>control block<\/i> which keeps track of the number of active references. In the case of C++\/CX, the control block consists of the following:<\/p>\n<ul>\n<li>A pointer-sized vtable for exposing the methods of <i>IWeakReference<\/i>.<\/li>\n<li>A 32-bit count of outstanding weak references.<\/li>\n<li>A 32-bit count of outstanding strong references.<\/li>\n<li>A pointer to the target of the weak reference, if still valid.<\/li>\n<\/ul>\n<p>If the target no longer exists, then the strong reference count is zero and the target pointer is null.<\/p>\n<p>In our case, we have a non-null target, so we can pull it out and find the target of the weak reference.<\/p>\n<pre>0:003&gt; dps 00000203`a7edb710 l1\r\n00000203`a7edb710  00007ffd`aee74450 contoso!Contoso::Widget::`vftable'\r\n<\/pre>\n<p>In this case, it&#8217;s a <code>Contoso::<\/code><code>Widget<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you ever have to debug one of these things.<\/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-103725","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>If you ever have to debug one of these things.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103725","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=103725"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103725\/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=103725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}