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