关闭

Avoid Memory Corruption when Assigning a CComBSTR to a CComVariant's bstrVal Member

标签: destructorstringup
763人阅读 评论(0) 收藏 举报
分类:

Although the CComBSTR = operator is overloaded to make a copy of the string, this is not the case when assigning a CComVariant's bstrVal member to a CComBSTR. In this case, you need to make an explicit copy:

CComVariant bstrTarget;
CComBSTR strSource("test");

// Use CComBSTR::Copy to make a copy
// of the source string.
bstrTarget.bstrVal = strSource.Copy();
If you don't make a copy of the source string, it will wind up being freed twice—once by the CComVariant's destructor, and once by the original CComBSTR's destructor. 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:294488次
    • 积分:2631
    • 等级:
    • 排名:第13890名
    • 原创:37篇
    • 转载:34篇
    • 译文:0篇
    • 评论:37条
    最新评论
    C++大牛