I am trying to track down some memory leaks in a COM, C++ application that
uses a large number of BSTRs. I read on MSDN that OleAut32.dll can cache
BSTRs, which will cause IMallocSpy-based memory trackers to report false
leaks. I think this is what is happening in the code sample presented below.
I tried setting a OANOCACHE system environment variable: OANOCACHE=1. But I
still see leaks reported by the simple code sample below. The only way I
have found to cause OleAut32.dll to flush its BSTR cache, is to call
CoUninitialize(). I am running this test on WinXP SP2. Can anyone help me
get OANOCACHE to work? Do I need a debug build of OleAut32.dll? (I read
that the OANOCACHE is built into the release build of WinXP SP2)
void main()
{
// Initialize IMallocSpy memory tracker
InitAllocTracking( /* some parameters */ );
BSTR s1 = SysAllocString(L"string 1");
SysFreeString(s1);
BSTR s2 = SysAllocString(L"string 2");
SysFreeString(s2);
BSTR s3 = SysAllocString(L"string 3");
SysFreeString(s3);
BSTR s4 = SysAllocString(L"string 4");
SysFreeString(s4);
SummarizeMemoryLeaks();
}