释放接口指针
Release Interface pointers
在类的析构函数中,我有一些智能指针被声明为类内的成员变量,同时释放这些接口,哪种代码更好:
成员可变
CComQIPtr<IMyInterFace> m_pMyInterface;
构造函数
m_pMyInterface.CreateInstace(CLSID_MyInterface);
在Destructor 中
if(m_pMyInterface)
m_pMyInterface.Release();
或者应该是
if(m_pMyInterface.p ! = NULL)
m_pMyInterface.Release();
形成上面哪一个更好,我使用接口指针的方式是否有任何缺陷。当做tom
~CComQIPtr
析构函数负责处理一切。您不需要明确发布。然而,如果您想重用变量,您可以执行以下两种操作之一:
m_pMyInterface.Release();
m_pMyInterface = NULL;
(运算符=
与CComPtr
一起工作,可能与CComQIPtr
不明确)
NULL检查是不必要的,CComQIPtr::Release
方法无论如何都会这样做。
它们是等价的,尽管我真的不喜欢有人仅仅为了它而写书。
此外,Release()
调用是不需要的,只需让析构函数处理即可
选择短的。
相关文章:
- 如何模板化堆栈分配的多态指针数组到接口,包括派生类型的相应点?
- 如何使用接口指针调用方法,该指针是其具体类的一部分,而不是接口的一部分
- 在接口中使用通用指针的原因/正确情况是什么
- 在接口文件中使用模板时出现"not a type"错误的函数指针
- 静态声明专用类的接口指针数组
- 指向模板接口的唯一指针
- 为什么在将多态行为与指向接口的指针一起使用时没有调用析构函数?
- 为什么在取消引用的指向接口的指针上使用赋值运算符不是编译器错误
- 如何找到C 接口指针的地址
- 指向接口 C++ 的指针
- 使用基类指针 - cpp 的适当声明设计接口
- 使用带有COM接口的STL智能指针
- 使用抽象接口C++ dll 边界 -标头中的智能指针>?叫删除?
- 将指向COM接口指针的指针从托管c++传递到.NET c#
- 当从一个应用程序调用时,在DLL方法中创建COM接口指针是有效的,但当从另一个应用软件调用时则无效
- 创建 COM 接口指针在开发计算机上有效,但会导致其他计算机上的应用程序崩溃
- C++ 当函数 arg 类型为类接口时,无法传递对指针的引用
- 如何在C#中使用C++DLL导出的接口指针
- 如何将共享_ptr用于com接口指针
- 如何获取 LPDISPATCH 指针/接口并将其传递给 activeX 控件