home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Troubleshooting Netware Systems
/
CSTRIAL0196.BIN
/
attach
/
msj
/
v10n10
/
oleq1195.exe
/
LEAKMAIN.CPP
< prev
Wrap
C/C++ Source or Header
|
1995-10-01
|
2KB
|
95 lines
//////////////////////////////////////////////////////
//
// LeakMain.cpp - Copyright 1995, Don Box
//
// Task Allocator test app
//
#include <windows.h>
#include "HeapDet.h"
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
CoInitialize(0);
// create a trace file
HANDLE hTrace = CreateFile(__TEXT("Logfile.txt"),
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
0);
// declare a heap detective object and register it
CoHeapDetective sherlock(hTrace);
SCODE scode = GetScode(CoRegisterMallocSpy(&sherlock));
if (scode == CO_E_OBJISREG)
MessageBox(0, __TEXT("Someone has already registered a MallocSpy."),
__TEXT("Drat!!"), MB_OK);
try
{
// allocate some raw memory and a BSTR
void *p = CoTaskMemAlloc(128);
BSTR bstr = SysAllocString(OLESTR("Hello, IMallocSpy"));
// try to realloc the raw memory
if (p)
{
void *temp = CoTaskMemRealloc(p, 128 * 2048);
if (temp)
p = temp;
}
// occasionally use a bad ptr, potentially corrupting the heap
if (GetTickCount() & 0x100)
{
DWORD *pdw = LPDWORD(p) + 10;
do
*(--pdw) = 12;
while (pdw >= LPDWORD(p)); // highly defective statement!
}
// free the raw memory
if (p)
CoTaskMemFree(p);
// try to realloc the BSTR
if (bstr)
SysReAllocStringLen(&bstr, OLESTR("Goodbye, Im Al Locspy"), 256);
// occasionally free the bstrs, potentially leaking
if (GetTickCount() & 0x10)
if (bstr)
SysFreeString(bstr);
}
catch (const CoHeapDetective::XCorruptedHeap&)
{
MessageBox(0, __TEXT("The heap has been corrupted."),
__TEXT("The Ugly!"), MB_OK);
}
CoRevokeMallocSpy();
CoUninitialize();
// display the final status of heap
DWORD cb = sherlock.GetBytesAlloced();
if (cb)
{
TCHAR buf[80];
wsprintf(buf, __TEXT("%u bytes were leaked from "
"the Task Allocator."), cb);
MessageBox(0, buf, __TEXT("The Bad"), MB_OK);
}
else
MessageBox(0, __TEXT("This application has a most hygenic heap."),
__TEXT("The Good"), MB_OK);
return 0;
}