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