home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / programm / 2321 < prev    next >
Encoding:
Text File  |  1992-08-14  |  3.6 KB  |  114 lines

  1. Newsgroups: comp.programming
  2. Path: sparky!uunet!gumby!wupost!uwm.edu!csd4.csd.uwm.edu!markh
  3. From: markh@csd4.csd.uwm.edu (Hunk)
  4. Subject: Re: When should a debugger be used?
  5. Message-ID: <1992Aug14.171601.25077@uwm.edu>
  6. Sender: news@uwm.edu (USENET News System)
  7. Organization: Computing Services Division, University of Wisconsin - Milwaukee
  8. References: <j1ymvg=.pdh@netcom.com> <164c9vINNijk@agate.berkeley.edu> <1992Aug12.233904.11067@thinkage.on.ca>
  9. Date: Fri, 14 Aug 1992 17:16:01 GMT
  10. Lines: 102
  11.  
  12. In article <1992Aug12.233904.11067@thinkage.on.ca> atbowler@thinkage.on.ca (Alan Bowler) writes:
  13. >Memory leaks are one of the areas where I have found debuggers to be
  14. >very little use.  I do use it to examine the free memory list, and say
  15. >to myself "this look suspiciously large", but that just tells me there
  16. >MIGHT be a leak.  Finding the actual is has always been more of
  17. >an analysis problem.
  18.  
  19. Memory leaks (malloc/free faults) are much, much easier to trace without a
  20. debugger, if you're using C.  Substitute your own diagnostic
  21. malloc/realloc/free routines, and do a free-list dump at appropriate times.
  22.  
  23. These are C diagnostic routines:
  24.  
  25. Mem.h:
  26. #if DEBUG_MALLOC
  27. #   define TABLE_SIZE 0x1000
  28.    extern void *_Malloc(char Tag, unsigned Size);
  29.    extern void *_Realloc(char Tag, void *X, unsigned Size);
  30.    extern void _Free(void *X);
  31.    extern void ShowAll(void);
  32. #   define Malloc(Tag, Size) _Malloc(Tag, Size)
  33. #   define Relloc(Tag, X, Size) _Realloc(Tag, X, Size)
  34. #   define Free(X) _Free(X)
  35. #else
  36. #   define Malloc(Tag, Size)    malloc(Size)
  37. #   define Relloc(Tag, X, Size) realloc(X, Size)
  38. #   define Free(X)              free(X)
  39. #   define ShowAll()
  40. #endif
  41.  
  42. Mem.c:
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45.  
  46. typedef void *addr;
  47. struct XX {
  48.    addr X; unsigned Size;
  49. } Tab[TABLE_SIZE], *TP = Tab;
  50.  
  51. struct XX *XP; char Flag;
  52.  
  53. void ShowAll(void) {
  54.    printf("PURGE:\n");
  55.    for (XP = Tab; XP < TP; XP++)
  56.       printf("%8x %4x\n", (int)XP->X, XP->Size);
  57.    if (TP > Tab) exit(0);
  58. }
  59.  
  60. addr _Malloc(char Tag, unsigned Size) {
  61.    addr X = malloc(Size);
  62.    printf("%c (%2d) NEW %d -> %8x\n", Tag, TP - Tab, Size, (int)X);
  63.    if (X == 0) return X;
  64.    for (XP = Tab; XP < TP; XP++) {
  65.       if (X == XP->X) printf("CLASH.\n"), exit(0);
  66.       if (X < XP->X + XP->Size && XP->X < X + Size)
  67.          printf("OVERLAP.\n"), exit(0);
  68.    }
  69.    if (TP - Tab >= TABLE_SIZE)
  70.       fprintf(stderr,"TOO MUCH FOR MEM TO HANDLE.\n"), exit(0);
  71.    TP->X = X, TP->Size = Size, TP++;
  72.    return X;
  73. }
  74.  
  75. addr _Realloc(char Tag, addr X, unsigned Size) {
  76.    struct XX *YP;
  77.    printf("%c (%2d) REA %d %8x -> ", Tag, TP - Tab, Size, (int)X);
  78.    if (X == 0) { XP = TP; goto FOUND; }
  79.    for (XP = Tab; XP < TP; XP++) {
  80.       if (X == XP->X) goto FOUND;
  81.       if (X < XP->X + XP->Size && XP->X < X + Size) printf("OVERLAP.\n"), exit(0);
  82.    }
  83.    printf("\nLOST.\n"), exit(0);
  84. FOUND:
  85.    X = realloc(X, Size);
  86.    printf("%8x\n", (int)X);
  87.    if (X == 0) return X;
  88.    for (YP = Tab; YP < TP; YP++) if (YP != XP) {
  89.       if (X == YP->X) printf("CLASH.\n"), exit(0);
  90.       if (X < YP->X + YP->Size && YP->X < X + Size) printf("OVERLAP.\n"), exit(0);
  91.    }
  92.    if (XP == TP) {
  93.       if (TP - Tab >= TABLE_SIZE) 
  94.          fprintf(stderr,"TOO MUCH FOR MEM.\n"), exit(0);
  95.       TP++;
  96.    }
  97.    XP->X = X, XP->Size = Size;
  98.    return X;
  99. }
  100.  
  101. void _Free(addr X) {
  102.    printf("(%2d) OLD %8x\n", TP - Tab, (int)X);
  103.    if (X == 0) { free(X); return; }
  104.    for (XP = Tab; XP < TP; XP++) {
  105.       if (X == XP->X) goto FOUND;
  106.       if (X < XP->X + XP->Size && XP->X < X) printf("OVERLAP.\n"), exit(0);
  107.    }
  108.    printf("LOST.\n"), exit(0);
  109. FOUND:
  110.    TP--, XP->X = TP->X, XP->Size = TP->Size;
  111.    free(X);
  112. }
  113.  
  114.