NateX’s Weblog

Just another WordPress.com weblog

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

leave a comment »

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. 

Alison Lomaka
Advertisement

Written by natex

December 2, 2008 at 8:17 am

Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.