|
|
1.1.1.3 ! root 1: ! 2: /******************************************************************************\ ! 3: * This is a part of the Microsoft Source Code Samples. ! 4: * Copyright (C) 1993 Microsoft Corporation. ! 5: * All rights reserved. ! 6: * This source code is only intended as a supplement to ! 7: * Microsoft Development Tools and/or WinHelp documentation. ! 8: * See these sources for detailed information regarding the ! 9: * Microsoft samples programs. ! 10: \******************************************************************************/ ! 11: 1.1 root 12: /*************************************************************************** 13: * * 14: * MODULE : mem.c * 15: * * 16: * PURPOSE : Functions for debugging memory allocation bugs. * 17: * * 18: ***************************************************************************/ 19: #include <windows.h> 20: 21: #define MAX_OBJECTS 200 22: 23: PSTR aptrs[MAX_OBJECTS]; 24: DWORD cptrs = 0; 25: 26: /**************************************************************************** 27: * * 28: * FUNCTION : DbgAlloc() * 29: * * 30: * PURPOSE : Useful routine for catching memory allocation errors. * 31: * Enters allocated objects into an array to check when freed * 32: * * 33: * RETURNS : pointer to object allocated. * 34: * * 35: ****************************************************************************/ 36: PSTR DbgAlloc( 37: register DWORD cb) 38: { 39: register PSTR p; 40: 41: p = (PSTR)LocalAlloc(LPTR, cb); 42: aptrs[cptrs++] = p; 43: if (cptrs >= MAX_OBJECTS) 44: OutputDebugString("Too many objects to track"); 45: return p; 46: } 47: 48: /**************************************************************************** 49: * * 50: * FUNCTION : DbgFree() * 51: * * 52: * PURPOSE : To free an object allocated with DbgAlloc(). Checks the * 53: * object array to make sure an object isn't freed twice. * 54: * * 55: * RETURNS : * 56: * * 57: ****************************************************************************/ 58: PSTR DbgFree( 59: register PSTR p) 60: { 61: register DWORD i; 62: 63: if (p == NULL) 64: return p; 65: 66: for (i = 0; i < cptrs; i++) { 67: if (aptrs[i] == p) { 68: aptrs[i] = aptrs[cptrs - 1]; 69: break; 70: } 71: } 72: if (i == cptrs) { 73: OutputDebugString("Free on non-allocated object"); 74: DebugBreak(); 75: } else { 76: LocalUnlock((HANDLE)p); 77: p = (PSTR)LocalFree((HANDLE)p); 78: } 79: cptrs--; 80: return p; 81: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.