静态 CComPtr 变量

发布于 2024-09-05 21:23:37 字数 82 浏览 9 评论 0原文

在应用程序中使用静态 CComPtr 成员变量是不是一个坏主意? 由于我们无法控制静态变量的销毁,并且它可能在 CoUninitialze 之后发生。

Is it bad idea to have static CComPtr member variables in an application.
Since we cannt control destruction of static variable and it can happen after CoUninitialze .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-残月青衣踏尘吟 2024-09-12 21:23:37

如果您采取适当的预防措施,那么使用 CComPtr 作为静态成员本质上并不是邪恶的。

我所说的“适当的预防措施”是指您应该考虑:

  • 互斥对它的访问;
  • 确保它已经初始化
    使用前;
  • 为您自己的类维护互斥的静态实例计数;
  • 确保 CComPtr::Release 是在您的班级自己的 FinalRelease 中调用当实例计数达到零时的方法。

Provided you take the appropriate precautions, then using a CComPtr as a static member is not inherently evil.

By "appropriate precautions", I mean you should consider:

  • Mutexing access to it;
  • Ensuring that it has been initialised
    before usage;
  • Maintining a mutexed, static instance count for your own class;
  • Ensuring that CComPtr::Release is called in your class' own FinalRelease method when the instance count reaches zero.
又爬满兰若 2024-09-12 21:23:37

无论如何,这是一个坏主意

it is a bad idea anyway

丢了幸福的猪 2024-09-12 21:23:37

正如谢尔盖在评论中所说,我认为这很糟糕。静态对象的析构函数在 main 终止后调用,如 C++03 标准第 3.6.3 节中所述:

静态存储持续时间(在块范围或名称空间范围声明)的已初始化对象的析构函数(12.4)作为从 main 返回的结果和调用 exit(18.3)的结果而被调用。这些对象按照其构造函数完成或动态初始化完成的相反顺序被销毁。如果静态初始化一个对象,则该对象将按照与动态初始化该对象相同的顺序销毁。对于数组或类类型的对象,该对象的所有子对象都会在子对象构造期间初始化的任何具有静态存储持续时间的本地对象被销毁之前被销毁。

如下演示: http://www.geeksforgeeks.org/static-objects-destroyed/

但是 CoUninitialize 其中在 Main 终止之前调用清理主线程上的 COM 库。 CoUninitialize 将清理所有剩余资源,如 msdn 文档中所述。我们应该在调用 CoUninitialize 之前调用 COM 对象的 Release 方法,因为之后它们将不再存在,因此我们应该确保对 CComPtr 析构函数的调用发生在调用 CoUninitialize 之前。因此,不应将 CComPtr 设为静态。

As Sergey said in his comment, I think it is bad. Destructors of static objects are called after main terminates as explained in section§ 3.6.3 of the C++03 standard:

Destructors (12.4) for initialized objects of static storage duration (declared at block scope or at namespace scope) are called as a result of returning from main and as a result of calling exit (18.3). These objects are destroyed in the reverse order of the completion of their constructor or of the completion of their dynamic initialization. If an object is initialized statically, the object is destroyed in the same order as if the object was dynamically initialized. For an object of array or class type, all subobjects of that object are destroyed before any local object with static storage duration initialized during the construction of the sub- objects is destroyed.

and as demoed here: http://www.geeksforgeeks.org/static-objects-destroyed/.

But CoUninitialize which cleans the COM library on the main thread is called before Main terminates. CoUninitialize will clean up all remaining resources as explained in the msdn documentation. We should call the Release method of COM objects before CoUninitialize is called because they won't exist anymore after and therefore we should make sure that calls to CComPtr destructor happen before CoUninitialize is called. Therefore a CComPtr should not be made static.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文