{"id":105996,"date":"2021-12-02T07:00:00","date_gmt":"2021-12-02T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=105996"},"modified":"2021-12-02T06:17:16","modified_gmt":"2021-12-02T14:17:16","slug":"20211202-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20211202-00\/?p=105996","title":{"rendered":"How do I receive ownership of a C-style array in a Windows Runtime component?"},"content":{"rendered":"<p>Last time, <a title=\"How can I transfer ownership of a C-style array to a Windows Runtime component?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20211201-00\/?p=105987\"> we looked at working around the inability to transfer ownership of a C-style array into a Windows Runtime method<\/a>, and we hit upon the idea of reversing the roles in order to use the <i>ReceiveArray<\/i> pattern. Last time, we looked at how this reversed method would be called. This time, we&#8217;ll implement it on the provider side.<\/p>\n<pre>namespace winrt::Sample::implementation\r\n{\r\n    struct Widget : WidgetT&lt;Widget&gt;\r\n    {\r\n        winrt::slim_mutex m_mutex;\r\n        winrt::com_array&lt;int32_t&gt; m_values;\r\n\r\n        void SetIndices(IndexProducer const&amp; producer)\r\n        {\r\n            auto values = producer();\r\n            auto lock = std::lock_guard(m_mutex);\r\n            m_values = std::move(values);\r\n        }\r\n    };\r\n}\r\n<\/pre>\n<p>The consumption is a bit tricky due to having to deal with locking.<\/p>\n<ul>\n<li>We don&#8217;t want to call into external code while holding the lock.<\/li>\n<li>Modifying the member variables must be done while holding the lock.<\/li>\n<\/ul>\n<p>This means that we need to order the operations carefully.<\/p>\n<p>First, we call the delegate outside the lock. That way, the delegate is free to enter any locks it wishes in order to produce the C-style array, which is then transferred to the <code>com_array<\/code>.<\/p>\n<p>Next, we enter the lock to prevent concurrent modification of the <code>Widget<\/code> object.<\/p>\n<p>Once safely inside the lock, we move-assign the C-style array into the <code>m_values<\/code> member, thereby transferring ownership of the data to the member variable. This achieves our goal of getting the data into the <code>m_values<\/code> without ever being copied.<\/p>\n<p>Next time, we&#8217;ll look at another weird corner case in the Windows Runtime that C-style arrays run up against.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The implementation side of the role reversal.<\/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-105996","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The implementation side of the role reversal.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105996","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=105996"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105996\/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=105996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=105996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=105996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}