I have the following code in C++. Do I need to free ‘varDest’ variable?
VARIANT val;
if(SUCCEEDED(classObj->Get(pwPropName, 0, &val, NULL, 0))) //WMI property retrieval
{
//Then at some point
VARIANT varDest;
varDest.vt = VT_EMPTY;
if(SUCCEEDED(::VariantChangeType(&varDest,
const_cast<VARIANT *>(&val), 0, VT_BSTR)))
{
//Do I need to call the following?
VariantClear(&varDest);
}
VariantClear(&val);
}
Yes you must call
VariantClear. TheVariantChangeTypemethod if successful will essentially coerce a copy of the source into the destination. This copy in the destination is now independently tracked and must be independently cleared.