当你把一个非敏捷对象放入 CoreApplication Properties 时会发生什么?

The Old New Thing (Raymond Chen) 新闻

摘要

Microsoft Old New Thing 博客文章解释了存储在 CoreApplication.Properties 中的非敏捷对象如何通过 RoGetAgileReference 被包装——导致它们在其宿主 apartment 关闭时过期,从而在 Properties 中留下一个中毒条目:HasKey 仍然报告其存在,但 Lookup 会以 E_INVALIDARG 拒绝请求。

<p>上次我们注意到,<a title="为什么 CoreApplication Properties 中的条目会在源应用程序关闭时过期?" href="https://devblogs.microsoft.com/oldnewthing/20261125-00/?p=112895/"> <code>Core­Application.<wbr />Properties</code> 中的非敏捷对象似乎与敏捷对象的行为不同</a>。</p> <p>我是说,它们看起来行为不同,但这只是因为你的视角太狭隘了。</p> <p>如果你把规则理解为“这些对象会一直存活到再没有任何 apartment 能访问它们为止”,那么它们的行为其实是一样的。非敏捷对象只能从创建它的 apartment 中使用,而敏捷对象可以在任何 apartment 中使用。因此,非敏捷对象在其宿主 apartment 关闭时过期,而敏捷对象则在<i>所有</i> apartment 都关闭时过期。</p> <p><code>Core­Application.<wbr />Properties</code> 使用 <code>Ro­Get­Agile­Reference</code> 来包装非敏捷对象,以便能够安全地存储它。(实际上,它对<i>所有</i>对象都使用 <code>Ro­Get­Agile­Reference</code> 进行包装,只不过对敏捷对象再包一层敏捷包装是空操作。)</p> <p>在内部,<code>Ro­Get­Agile­Reference</code> 会根据你传递的标志使用多种机制来创建敏捷引用,但它们都有一个共同特性:对非敏捷对象的引用会在 apartment 关闭时被释放。我们观察到的行为来自 <code>Ro­Get­Agile­Reference</code>,而不是来自 <code>Core­Application.<wbr />Properties</code>。</p> <p>现在,<code>Properties</code> 集合并不知道这个敏捷引用指向的对象已经过期了。它只是持有这个敏捷引用,等待在被请求时返回它。</p> <p>那么,让我们尝试访问这个过期的对象,看看会发生什么。</p> <pre>#include &lt;stdio.h&gt; #include &lt;winrt/Windows.ApplicationModel.Core.h&gt; #include &lt;winrt/Windows.Foundation.Collections.h&gt; <span style="border: solid 1px currentcolor; border-bottom: none;">struct NonAgile : winrt::implements&lt;NonAgile, </span> <span style="border: 1px currentcolor; border-style: none solid;"> winrt::Windows::Foundation::IInspectable, winrt::non_agile&gt;</span> <span style="border: 1px currentcolor; border-style: none solid;">{ </span> <span style="border: 1px currentcolor; border-style: none solid;"> ~NonAgile() { printf("Expired!\n"); } </span> <span style="border: solid 1px currentcolor; border-top: none;">}; </span> int main() { winrt::init_apartment(winrt::apartment_type::single_threaded); std::thread t([] { winrt::init_apartment(winrt::apartment_type::single_threaded); winrt::Windows::ApplicationModel::Core::CoreApplication::Properties(). Insert(L"meow", <span style="border: solid 1px currentcolor;">winrt::make&lt;NonAgile&gt;()</span>); winrt::uninit_apartment(); }); t.join(); <span style="border: solid 1px currentcolor; border-bottom: none;">bool present = </span> <span style="border: 1px currentcolor; border-style: none solid;">winrt::Windows::ApplicationModel::Core::CoreApplication::Properties().</span> <span style="border: 1px currentcolor; border-style: none solid;"> HasKey(L"meow"); </span> <span style="border: solid 1px currentcolor; border-top: none;">printf("Has the value: %s\n", present ? "true" : "false"); </span> <span style="border: solid 1px currentcolor; border-bottom: none;">auto value = </span> <span style="border: 1px currentcolor; border-style: none solid;"> winrt::Windows::ApplicationModel::Core::CoreApplication::Properties().</span> <span style="border: 1px currentcolor; border-style: none solid;"> Lookup(L"meow"); </span> <span style="border: solid 1px currentcolor; border-top: none;">printf("Has a value? %s\n", value ? "true" : "false"); </span> winrt::uninit_apartment(); return 0; } </pre> <p>我在非敏捷对象析构时添加了一条消息,这样你就可以(通过单步调试代码)看到它发生在辅助线程调用 <code>uninit_<wbr />apartment</code> 关闭其单线程 apartment 的时候。这是 Global Interface Table 在 apartment 关闭时释放其引用。</p> <p>在我们将非敏捷对象放入 <code>Properties</code> 并关闭其 apartment 之后,非敏捷对象被销毁了,但条目仍然留在 <code>Properties</code> 中——我们可以通过 <code>HasKey()</code> 仍然返回 <code>true</code> 来确认这一点。该键值对仍然存在于 <code>Properties</code> 中。</p> <p>然而,如果我们尝试取回该值,<code>Lookup</code> 会因异常而失败,因为该条目现在已中毒:它是一个指向已被销毁对象的引用。¹ 你无法取回中毒的条目;你唯一能做的就是删除它。</p> <p>下次,我们将探讨这一切如何融入最初的问题:创建一个假的敏捷包装器——它在技术上是敏捷的,但由于原始对象拒绝被编组,它在宿主 apartment 之外毫无用处。</p> <p>¹ 返回的具体错误是 <code>E_<wbr />INVALID­ARG</code>,因为 Global interface Table 条目已不再有效。这个特定的错误代码是一个泄漏的抽象。如果开发者当初考虑过这一点,他们本可以添加代码将该错误转换为更合适的内容,比如 <code>RPC_E_SERVER_DIED</code>。</p> <p>文章 <a href="https://devblogs.microsoft.com/oldnewthing/20261126-00/?p=112899/">What happens when you put a non-agile object in the CoreApplication Properties?</a> 最初发布在 <a href="https://devblogs.microsoft.com/oldnewthing">The Old New Thing</a> 上。</p>
查看原文
查看缓存全文

缓存时间: 2026/11/27 03:12

# 当你把一个非 agile 对象放进 CoreApplication Properties 时会发生什么? - The Old New Thing Source: https://devblogs.microsoft.com/oldnewthing/20261126-00/?p=112899/ 上次我们注意到,`CoreApplication.Properties` 中的非 agile 对象看起来与 agile 对象表现不同(https://devblogs.microsoft.com/oldnewthing/20261125-00/?p=112895/)。我是说,看起来它们的行为确实不一样,但这只是因为你看待问题的视角太狭窄了。如果你把规则理解为“对象会一直存活,直到再也没有公寓(apartment)能够访问它们为止”,那它们的行为其实完全相同。非 agile 对象只能在创建它的公寓中使用,而 agile 对象可以在任意公寓中使用。因此,非 agile 对象在其宿主公寓关闭时就会过期,而 agile 对象则要等到*所有*公寓都关闭才会过期。 `CoreApplication.Properties` 使用 `RoGetAgileReference` 来包装非 agile 对象,以便能够安全地存储它。(实际上,它对*所有*对象都会调用 `RoGetAgileReference` 来包装,只不过给 agile 对象再套一层 agile 包装是个空操作(nop)。) 在内部,`RoGetAgileReference` 会根据你传入的标志采用不同的机制来创建 agile 引用,但它们都有一个共同的特性:对非 agile 对象的引用会在公寓关闭时被释放。我们观察到的行为来自 `RoGetAgileReference`,而不是来自 `CoreApplication.Properties`。 现在,`Properties` 集合并不知道这个 agile 引用指向的是一个已过期的对象。它只是持有这个 agile 引用,等待在被请求时返回它。那么我们试着访问一下这个过期的对象,看看会发生什么。 ``` #include #include #include struct NonAgile : winrt::implements { ~NonAgile() { printf("Expired!\n"); } }; int main() { winrt::init_apartment(winrt::apartment_type::single_threaded); std::thread t([] { winrt::init_apartment(winrt::apartment_type::single_threaded); winrt::Windows::ApplicationModel::Core::CoreApplication::Properties(). Insert(L"meow", winrt::make()); winrt::uninit_apartment(); }); t.join(); bool present = winrt::Windows::ApplicationModel::Core::CoreApplication::Properties(). HasKey(L"meow"); printf("Has the value: %s\n", present ? "true" : "false"); auto value = winrt::Windows::ApplicationModel::Core::CoreApplication::Properties(). Lookup(L"meow"); printf("Has a value? %s\n", value ? "true" : "false"); winrt::uninit_apartment(); return 0; } ``` 我在非 agile 对象析构时加了一条消息,这样你就能(通过单步跟踪代码)看到它是在辅助线程调用 `uninit_apartment` 关闭其单线程公寓时发生的。这是全局接口表(Global Interface Table)在公寓关闭时释放其引用。 在我们把非 agile 对象放入 `Properties` 并关闭它的公寓之后,该非 agile 对象被销毁了,但条目仍然留在 `Properties` 中——我们可以通过 `HasKey()` 仍然返回 `true` 来确认这一点。这个键值对依然存在于 `Properties` 里。然而,如果我们尝试取回该值,`Lookup` 会抛出异常而失败,因为这个条目现在已经被“毒化”(poisoned)了:它引用的是一个已被销毁的对象。1 你无法取回被毒化的条目;唯一能做的就是删除它。 下次,我们将探讨这一切如何融入最初的那个问题:创建一个“假的”agile 包装器——它在技术上确实是 agile 的,但由于原始对象拒绝被封送(marshaled),因此在它的宿主公寓之外毫无用处。 1 这里返回的具体错误是 `E_INVALIDARG`,因为全局接口表的条目已不再有效。这个特定的错误码是一个“有漏洞的抽象”(leaky abstraction)。如果当初的开发人员考虑到这一点,他们本可以加上代码,把错误转换成更合适的值,比如 `RPC_E_SERVER_DIED`。 ### Category ### Topics ## Author Raymond Chen Raymond 参与 Windows 的演进已有 30 多年。2003 年,他开办了一个名为 The Old New Thing 的网站,其受欢迎程度远远超出了他最狂野的想象——这一发展至今仍让他心惊胆战。该网站后来衍生出一本书,书名巧合地也叫 The Old New Thing(Addison Wesley 2007)。他偶尔会出现在 Windows Dev Docs 的 Twitter 账号上,讲一些毫无用处的故事。

相似文章