MEM.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:3k
源码类别:

Windows编程

开发平台:

Visual C++

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