# 创建一个假的敏捷包装器:它在技术上是敏捷的,但在其原始单元之外毫无用处,第1部分 - The Old New Thing
来源:https://devblogs.microsoft.com/oldnewthing/20260803-00?p=112582
2026年8月3日
celebrate1 reaction
上次,我们讨论了当上下文回调失败(https://devblogs.microsoft.com/oldnewthing/20260731-00/?p=112578)时意味着什么,它使我们无法在原始上下文中释放对象。我们指出,问题在于当原始单元关闭时,我们也就失去了释放对象的机会。
我们想要的是介于强引用和弱引用之间的某种东西。我们想要一个强引用,但在来源单元关闭时,它会释放对目标的引用。
有这样的东西吗?
事实证明,确实有。
我们可以做的,是将对象注册到全局接口表(历史上称为GIT,与源代码控制系统无关)。通常这样做是为了通过兑现注册cookie来允许从另一个单元访问该对象。我们并不打算从另一个单元访问对象,而是想利用GIT的一个特性:当对象的单元关闭时,GIT中的引用会被自动释放。注册cookie仍然有效,但如果你试图兑现它,你会被告知服务器已不再可用。
所以这里的想法是,将原始委托注册到GIT中,并把它保存在敏捷包装器里。之后,敏捷包装器在销毁时取消注册该委托。我们从不兑现注册cookie。注册委托的目的不是从另一个单元访问它,而仅仅是在原始单元关闭时自动释放它。
那么让我们试一试。
下次再说。
### 类别
### 主题
## 作者
Raymond Chen
Raymond 参与 Windows 的演进已有30多年。2003年,他创建了一个名为 The Old New Thing 的网站,其受欢迎程度远远超出了他最狂野的想象,这一发展至今仍让他感到不寒而栗。该网站还衍生了一本书,书名也恰好是 The Old New Thing(Addison Wesley 2007)。他偶尔会出现在 Windows Dev Docs 的 Twitter 账户上,讲述一些毫无信息量的故事。
## 接下来阅读
## 保持关注
在新文章发布时获得通知。
关注此博客
- https://twitter.com/ChenCravat
- youtube (https://www.youtube.com/playlist?list=PLlrxD0HtieHge3_8Dm48C0Ns61I6bHThc)
- https://github.com/oldnewthing
- https://devblogs.microsoft.com/oldnewthing/feed/
Raymond Chen continues his series on creating a fake agile wrapper for non-marshalable COM objects by wrapping them in a marshalable object, enabling registration in the global interface table while preserving undefined-behavior-free failures in other apartments.
Raymond Chen continues his series on creating a fake agile wrapper in COM, explaining how to move non-marshalable objects by forwarding references into the force_marshal wrapper and discussing C++/WinRT deduction guides.
Raymond Chen explains how to use std::unique_ptr to manage a registration cookie in a fake agile wrapper, discussing integer-to-pointer round-tripping and the wil::unique_any alternative.