[英]Understanding CComBSTR assignment operators
Say I have the following: 说我有以下内容:
BSTR myBSTR = SysAllocString( L"MYBSTR" );
CComBSTR myCComBSTR = myBSTR;
Does myCComBSTR
take ownership of myBSTR
and free it when it goes out of scope? 是否
myCComBSTR
采取的所有权myBSTR
和免费当它超出范围? Or does it make a copy of myBSTR
and produce a memory leak if i dont free myBSTR
? 或者,如果我不释放
myBSTR
,它会复制myBSTR
并产生内存泄漏吗?
If this produces a memory leak, what's the most efficient way of handling this? 如果这会产生内存泄漏,那么处理此问题的最有效方法是什么? (
myBSTR
will be passed in to a function as a BSTR
and i want to store it as a CComBSTR
internally) (
myBSTR
将作为BSTR
传递给函数,我想在内部将其存储为CComBSTR
)
In this case the CComBSTR
instance creates an independent copy. 在这种情况下,
CComBSTR
实例创建一个独立的副本。 You will need to manually free myBSTR
to avoid a leak. 您需要手动释放
myBSTR
以避免泄漏。
The simplest approach to fix this scenario is to skip the middle man SysAllocString
function 修复此场景的最简单方法是跳过中间人
SysAllocString
函数
CComBSTR myCComBSTR = L"MYBSTR";
On the other hand if you have a BSTR
and want to have a CComBSTR
take owner ship of it then use attach method. 另一方面,如果你有一个
BSTR
并想让CComBSTR
取得它的所有者,那么使用attach方法。 This method transfers ownership of the resource from the source BSTR
to the CComBSTR
instance. 此方法将资源的所有权从源
BSTR
转移到CComBSTR
实例。
CComBSTR myCComBSTR;
myCComBSTR.Attach(myBSTR);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.