Tuesday, August 16, 2011

C++ Tips and tricks

if a variance is declared, it needed to be cleared before delete it to release memory

CComVariant varResult;


CComVariant* pvars = new CComVariant[2];

VariantClear(&varResult);


VariantClear(&pvars[0]);

VariantClear(&pvars[1]);

delete[] pvars;

// When using SysAllocString or AllocSysString to create a BSTR string the SysFreeString needs to be called to delete the allocated BSTR.
// If the BSTR resources are not freed it will remain in memory even when the method goes out of scope.
// For more information refer to http://msdn.microsoft.com/en-us/library/xda6xzx7(v=vs.71).aspx.   *ListOfTests = ::SysAllocString(strTests.AllocSysString()); should be
*ListOfTests = strTests.AllocSysString();

BSTR windowCaption = strCap.AllocSysString();


BSTR sTime = strTime.AllocSysString();

BSTR filter = strFilter.AllocSysString();

BSTR logText = strText.AllocSysString();Fire_OnPrintMsg (windowCaption, sTime, filter, logText);
::SysFreeString(windowCaption);

::SysFreeString(sTime);

::SysFreeString(filter);

::SysFreeString(logText);