home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / ipc / ddeml / client / mem.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  4KB  |  82 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples.
  4. *       Copyright (C) 1993-1997 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.  
  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. PTSTR 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. PTSTR DbgAlloc(
  37. register DWORD cb)
  38. {
  39.     register PTSTR p;
  40.  
  41.     p = (PTSTR)LocalAlloc(LPTR, cb);
  42.     aptrs[cptrs++] = p;
  43.     if (cptrs >= MAX_OBJECTS)
  44.         OutputDebugString(TEXT("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. PTSTR DbgFree(
  59. register PTSTR 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(TEXT("Free on non-allocated object"));
  74.         DebugBreak();
  75.     } else {
  76.         LocalUnlock((HANDLE)p);
  77.         p = (PTSTR)LocalFree((HANDLE)p);
  78.     }
  79.     cptrs--;
  80.     return p;
  81. }
  82.