zar zar - 1 year ago 95
C++ Question

How to clean up _variant_t

My leak checker tells me there is 2nd chance exception in this function.

BOOL CADORecordset::SetFieldValue(LPCTSTR lpFieldName, CString strValue)
_variant_t vtFld;

vtFld.vt = VT_BSTR;
vtFld.vt = VT_NULL;

vtFld.bstrVal = strValue.AllocSysString();

BOOL bret = PutFieldValue(lpFieldName, vtFld);

return bret;

has a type BSTR member (
). We know that BSTR needs to be de-allocated using
which is what is done in the code above but since this BSTR is member of
who has its own destructor which cleans up, who should really clean up the
member in this case?

inline _variant_t::~_variant_t() throw()

It appears to me that this tries to clean up the memory again which is already cleaned up by
causing the exception? The documentation says it clears the variant but it is not clear what exactly it clears, does it free up the
as well?

If I remove the call
this does remove the 2nd chance exception but I really want to know that's the right thing to do because the documentation doesn't give enough confidence.

Answer Source

_variant_t automatically cleans up its data when it goes out of scope. You are manually calling SysFreeString(), but you are not setting bstrVal to NULL or setting vt to VT_EMPTY afterwards. So, when the variant_t destructor calls VariantClear() to clean up the data, it tries to free the bstrVal again and crashes.

So, simply do not call SysFreeString() manually at all. If you ever need to reset a variant_t manually, use its Clear() method instead: