home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n10 / oleq1195.exe / LEAKMAIN.CPP < prev   
C/C++ Source or Header  |  1995-10-01  |  2KB  |  95 lines

  1. //////////////////////////////////////////////////////
  2. // 
  3. // LeakMain.cpp - Copyright 1995, Don Box 
  4. //
  5. // Task Allocator test app
  6. //
  7.  
  8. #include <windows.h>
  9.  
  10. #include "HeapDet.h"
  11.  
  12. int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  13. {
  14.     CoInitialize(0);
  15.  
  16. // create a trace file
  17.     HANDLE hTrace = CreateFile(__TEXT("Logfile.txt"), 
  18.                               GENERIC_WRITE | GENERIC_READ,
  19.                               FILE_SHARE_READ | FILE_SHARE_WRITE,
  20.                               0,
  21.                               CREATE_ALWAYS,
  22.                               FILE_ATTRIBUTE_NORMAL,
  23.                               0);
  24.  
  25. // declare a heap detective object and register it
  26.     CoHeapDetective sherlock(hTrace);
  27.     SCODE scode = GetScode(CoRegisterMallocSpy(&sherlock));
  28.   if (scode == CO_E_OBJISREG)
  29.     MessageBox(0, __TEXT("Someone has already registered a MallocSpy."), 
  30.                   __TEXT("Drat!!"), MB_OK);
  31.  
  32.     try 
  33.     {
  34. // allocate some raw memory and a BSTR
  35.         void *p = CoTaskMemAlloc(128);
  36.         BSTR bstr = SysAllocString(OLESTR("Hello, IMallocSpy"));
  37.  
  38. // try to realloc the raw memory
  39.         if (p) 
  40.         {
  41.             void *temp = CoTaskMemRealloc(p, 128 * 2048);
  42.             if (temp)
  43.                 p = temp;
  44.         }
  45.  
  46. // occasionally use a bad ptr, potentially corrupting the heap
  47.         if (GetTickCount() & 0x100) 
  48.       {
  49.         DWORD *pdw = LPDWORD(p) + 10;
  50.         do 
  51.           *(--pdw) = 12;  
  52.         while (pdw >= LPDWORD(p)); // highly defective statement!
  53.       }
  54.  
  55. // free the raw memory
  56.         if (p)
  57.             CoTaskMemFree(p);
  58.  
  59. // try to realloc the BSTR
  60.         if (bstr)
  61.             SysReAllocStringLen(&bstr, OLESTR("Goodbye, Im Al Locspy"), 256);
  62.  
  63.       
  64.  
  65.  
  66. // occasionally free the bstrs, potentially leaking
  67.         if (GetTickCount() & 0x10) 
  68.           if (bstr)
  69.               SysFreeString(bstr);
  70.     
  71.     }
  72.     catch (const CoHeapDetective::XCorruptedHeap&)
  73.     {
  74.         MessageBox(0, __TEXT("The heap has been corrupted."), 
  75.                   __TEXT("The Ugly!"), MB_OK);
  76.     }
  77.  
  78.     CoRevokeMallocSpy();
  79.     CoUninitialize();
  80.  
  81. // display the final status of heap 
  82.     DWORD cb = sherlock.GetBytesAlloced();
  83.     if (cb)
  84.     {
  85.         TCHAR buf[80];        
  86.         wsprintf(buf, __TEXT("%u bytes were leaked from "
  87.                               "the Task Allocator."), cb);
  88.         MessageBox(0, buf, __TEXT("The Bad"), MB_OK);
  89.     }
  90.     else
  91.         MessageBox(0, __TEXT("This application has a most hygenic heap."),
  92.                   __TEXT("The Good"), MB_OK);
  93.     return 0;
  94. }
  95.