#include "stdafx.h" #include #ifndef NDEBUG # include # include "cmallspy.h" #endif static void test_com_allocs() { // // Create a couple of BSTRs. This shouldn't leak. // _bstr_t bstr; bstr = "abc"; bstr = "def"; bstr = "ghi"; // // The following copy() should leak and is identified // as COM allocation [4] in CMallocSpy ... // BSTR tmpstr = bstr.copy(); // // Create a VARIANT // _variant_t v; v = "some variant-wrapped string"; // // The following Detach() should leak and is identified // as COM allocation [5] in CMallocSpy ... // VARIANT tmp; tmp = v.Detach(); } static void test_crt_allocs() { char* cp; // // The following should not leak // cp = new char[50]; delete cp; // // The following should leak and is identified as // CRT allocation [48] ... // cp = new char[75]; } int main(int argc, char* argv[]) { // ******************** Test COM memory checker ****************** // ******************** Test COM memory checker ****************** // ******************** Test COM memory checker ****************** #ifndef NDEBUG // // Initialize COM. Also create first BSTR. This seems to create // a one-time (non-recurring) memory leak. We do it here so it // doesn't show up when we dump the COM memory leaks later. // ::CoInitialize(NULL); _bstr_t bstr("123"); // // Initialize the COM memory checker ... // CMallocSpy* pMallocSpy = new CMallocSpy; pMallocSpy->AddRef(); ::CoRegisterMallocSpy(pMallocSpy); pMallocSpy->Clear(); // pMallocSpy->SetBreakAlloc(4); // enable this if you want the debugger // to break at COM allocation 4 #endif test_com_allocs(); #ifndef NDEBUG // // Dump COM memory leaks // pMallocSpy->Dump(); // // Unregister the malloc spy ... // ::CoRevokeMallocSpy(); pMallocSpy->Release(); ::CoUninitialize(); #endif // ******************** Test CRT memory checker ****************** // ******************** Test CRT memory checker ****************** // ******************** Test CRT memory checker ****************** #ifndef NDEBUG // // Initialize the C/C++ run-time memory checker ... // _CrtMemState state; _CrtMemCheckpoint(&state); // _CrtSetBreakAlloc(48); // enable this if you want the debugger // to break at CRT allocation 48 #endif test_crt_allocs(); #ifndef NDEBUG // // Dump CRT memory leaks // _CrtMemDumpAllObjectsSince(&state); #endif return 0; }