- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对 COM 智能指针类 CComPtr
的使用有疑问。我正在关注 documentation 中的示例并且代码似乎没有在 CComPtr
上调用 CoCreateInstance()
(它只是在为其赋值之前对其进行声明)。
所以我写了一个这样的测试程序:
#include "stdafx.h"
#include "atlbase.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
CComPtr<int> myint = nullptr;
if (myint == nullptr) {
std::cout << "yes" << std::endl;
}
return 0;
}
它在 visual-studio 2013 中给出了以下错误:
------ Build started: Project: ConsoleApplication2, Configuration: Debug Win32 ------ ConsoleApplication2.cppc:\program files (x86)\microsoft visual studio 12.0\vc\atlmfc\include\atlcomcli.h(177): error C2227: left of '->Release' must point to class/struct/union/generic type type is 'int *' c:\program files (x86)\microsoft visual studio 12.0\vc\atlmfc\include\atlcomcli.h(175) : while compiling class template member function 'ATL::CComPtrBase::~CComPtrBase(void) throw()' with [ T=int ] c:\users\calvi_000\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp(18) : see reference to function template instantiation 'ATL::CComPtrBase::~CComPtrBase(void) throw()' being compiled with [ T=int ] c:\program files (x86)\microsoft visual studio 12.0\vc\atlmfc\include\atlcomcli.h(317) : see reference to class template instantiation 'ATL::CComPtrBase' being compiled with [ T=int ] c:\users\calvi_000\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp(10) : see reference to class template instantiation 'ATL::CComPtr' being compiled========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
为什么将 nullptr
分配给 CComPtr
对象是非法的?有什么方法可以用来检查 CComPtr
是否拥有任何对象?像 if (myint == nullptr)
这样的调用是否足以检查此智能指针是否不拥有任何对象?
最佳答案
Why is it illegal to assign a nullptr to a CComPtr object ?
事实并非如此。正如 Hans 所指出的,CComPtr
只能与 COM 接口(interface)一起使用,int
不是兼容类型。这就是编译器错误告诉您的内容:
error C2227: left of '->Release' must point to class/struct/union/generic type type is 'int *'
So the problem is not that you are assigning nullptr
, but that int
does not have a Release()
method, which CComPtr
requires. An IUnknown
(or derived) interface does have a Release()
method.
Is there any method that we can use to check whether or not a
CComPtr
owns any objects ? Will a call likeif (myint == nullptr)
enough to check if this smart pointer does not own any object ?
If you read the CComPtr
documentation more carefully, you would see that CComPtr
derives from CComPtrBase
, which implements operator!()
, operator T*()
and operator==()
operators (so CComPtr
will act like a raw pointer), as well as an IsEqualObject()
method:
int _tmain(int argc, _TCHAR* argv[])
{
CComPtr<IUnknown> myUnk = nullptr;
if (!myUnk) { // calls myUnk.operator!() ...
std::cout << "null" << std::endl;
else
std::cout << "not null" << std::endl;
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
CComPtr<IUnknown> myUnk = nullptr;
if (myUnk) { // calls myUnk.operator IUnknown*() ...
std::cout << "not null" << std::endl;
else
std::cout << "null" << std::endl;
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
CComPtr<IUnknown> myUnk = nullptr;
if (myUnk == nullptr) { // calls myUnk.operator==(IUnknown*) ...
std::cout << "null" << std::endl;
else
std::cout << "not null" << std::endl;
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
CComPtr<IUnknown> myUnk = nullptr;
if (myUnk.IsEqualObject(nullptr)) {
std::cout << "null" << std::endl;
else
std::cout << "not null" << std::endl;
}
return 0;
}
关于c++ - 测试 CComPtr 在 COM 中是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30902078/
我正在将 VS2008(VC++) 代码迁移到 VS2013。我收到以下错误: error C2664: 'HRESULT _CopyItfFromAdaptItf::copy(T **,ATL::C
我有一个成员变量声明为 CComPtr m_spXMLDoc; XML 文档是这样创建的 CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC
我正在使用 CComPtr 类型的对象.但是我有一些内存泄漏问题。特别是,我有以下代码: CComPtr bitmap = create_bitmap(bitmapSize); auto n = co
我有一种情况需要传递 CComPtr通过引用到一个函数。被调用参数是否从被调用参数中获取所有权(或)引用计数增加? void foo( CComPtr & pReader ) { // There
我使用 crtdbg 检测泄漏位置,在调用 new 时出现内存泄漏 CComPtr m_spColumnInfo CComPtr spResult = new CDBColumnInfo(); //M
我是 COM 和智能指针的新手,我正在尝试将项目从原始指针转换为 CComPtr 以避免内存管理的麻烦。我正在寻找一些关于如何在一般情况下正确使用 CComPointers 的功能和范围的建议。我的代
我不确定这种在函数内部使用 CComPtr 的方式,该函数的参数表示为双指针: HRESULT D3DPresentEngine::CreateD3DSample( IDirect3DSwap
我在我的类中定义了以下 CComPtr 对象和方法: private: CComPtr& getRawPdu(); // Returns the RawPdu interface po
这里有个简单的问题 假设我们有一个使用 shared_ptr/enable_shared_from_this 的代码库。 我们被要求将代码库的一部分转换为 COM 服务器,我们应该在其中使用 CCom
我正在使用 CComPtr 保存指向 com 对象的指针。 我还用类包装了资源: class ResourceWrapper { public: ResourceWrapper() {
当 CComPtr 作为类的成员变量时,如何销毁它? 下面是我写的一段代码,m_piControl是成员变量CComPtr。我已将 m_piControl 的值分配给 rpControl 和 rpCo
我有点习惯通过 COM 进行引用计数的概念,但我对 shared_ptr 有点陌生。我在 shared_ptr 中没有找到 CComPtr 的几个不错的属性,我想知道防止误用 shared_ptr 的
我正在做一些 COM 编程,我的编译器是 gcc。这意味着 CComPtr 对我不可用。如何使用 Boost 轻松实现替换?我需要的语义是: 复制时调用AddRef 销毁时调用Release 额外:能
在应用程序中使用静态 CComPtr 成员变量不是个好主意吗?由于我们无法控制静态变量的销毁,它可能发生在 CoUninitialze 之后。 最佳答案 如果您采取适当的预防措施,那么使用 CComP
我觉得..Attach 和 Detach 不会改变引用计数。但是 CComPtr 的析构函数在它包含的指针上调用释放。 那么有没有必要在一个use attach的时候每次都调用detach呢?...
我正在编写一个 C++/CLI 类,需要在其生命周期内保持 CComPtr,例如 public ref class MyClass { public: MyClass() {
我只想知道 ComPtr 之间的确切区别和 CComPtr ,以及是否 ComPtr::As()是 CComPtr::QueryInterface() 的类似物? 我阅读了两者的文档,但这个问题没有明
当用户按下登录按钮时调用 StartComObjects 函数,当用户按下取消按钮时调用 StopComObjects 函数。 StartComObjects 函数使用 CComPtr.CoCreat
我在尝试将基于 directshow 的直播播放器编写为 ActiveX 控件时遇到了问题。 我先用VS2010创建了一个MFC ActiveX控件工程(VC++类下),并在控件上附加了一个对话框,然
我对 COM 智能指针类 CComPtr 的使用有疑问。我正在关注 documentation 中的示例并且代码似乎没有在 CComPtr 上调用 CoCreateInstance()(它只是在为其赋
我是一名优秀的程序员,十分优秀!