home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / numega / sc501.exe / data1.cab / Examples / MEMORY.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-11-25  |  5.1 KB  |  217 lines

  1. (*
  2.  * Memory.PAS
  3.  * $Header: /Old/bcsample/BUGBNCHX/DLPHIERR/MEMORY.PAS 2     8/28/96 11:02a Bob $
  4.  *
  5.  * Description:
  6.  *     File includes routines for the LeakCheck and WriteCheck options
  7.  *     on the tree control.
  8.  *
  9.  * Notes:
  10.  *  <implementation notes go here>
  11.  *
  12.  ***********************************************************************
  13.  *
  14.  * Nu-Mega Technologies, Inc.
  15.  * P.O. Box 7780
  16.  * Nashua, NH 03060
  17.  *
  18.  * (c) Copyright 1994, 1995 Nu-Mega Technologies, Inc.
  19.  * ALL RIGHTS RESERVED.
  20.  *
  21.  ***********************************************************************
  22.  *
  23.  **********************************************************************)
  24.  
  25. (*
  26.    AllocMem_Leak allocates a pointer using the function AllocMem and does
  27.    not free it.  This error will appear at the end of the BoundsChecker
  28.    session.
  29. *)
  30. procedure AllocMem_Leak;
  31. var
  32.   testPointer: Pointer;
  33. begin
  34.   testPointer := AllocMem ( 25 );
  35. end;
  36.  
  37. (*
  38.    StrAlloc_Leak allocates a string through StrAlloc.  This string is not
  39.    freed.  This error will appear at the end of the BoundsChecker session.
  40. *)
  41. procedure StrAlloc_Leak;
  42. var
  43.   testPChar: PChar;
  44. begin
  45.   testPChar := StrAlloc ( 5 );
  46. end;
  47.  
  48. (*
  49.    StrNew allocates a string that the user must free.  If the user does
  50.    not, it is reported as a memory leak by BoundsChecker.
  51. *)
  52. procedure MemoryLeak_StrNew;
  53. var
  54.    testString :PChar;
  55. begin
  56.    testString := StrNew ( 'This is a leak ');
  57. end;
  58.  
  59.  
  60. (*
  61.    BitmapLeak creates/loads a bitmap which is not destroyed upon program
  62.    termination.  This leads to low resources.  This error is reported at the
  63.    end of the BoundsChecker session.
  64. *)
  65. procedure BitmapLeak;
  66. var
  67.    hBitmap: THandle;
  68. begin
  69.    hBitmap := CreateBitmap(80,80,1,8,NIL);
  70. end;
  71.  
  72. (*
  73.    MenuLeak creates/loads a menu which is not destroyed upon program
  74.    termination.  This leads to low resources.  This error is reported at the
  75.    end of the BoundsChecker session.
  76. *)
  77. procedure MenuLeak;
  78. var
  79.    hMenu: THandle;
  80. begin
  81.    hMenu := CreateMenu;
  82. end;
  83.  
  84. (*
  85.    DynamicOverrun is when a dynamically allocated piece of memory is overrun.
  86.    BoundsChecker will catch this type of overrun, reporting which allocated
  87.    block was overrun.
  88. *)
  89. procedure DynamicOverrun;
  90. var
  91.    count : Integer;
  92.    Overrun2: PChar;
  93. begin
  94.    try
  95.       Overrun2 := StrAlloc (4);
  96.       for count := 1 TO 5 do
  97.          Overrun2[count] := 'x';
  98.       StrDispose ( Overrun2 );
  99.    except
  100.    end;
  101. end;
  102.  
  103. (*
  104.    Stack variables can be overrun as well, causing stack corruption leading
  105.    to GPFs.  BoundsChecker will catch this error before the offending
  106.    instruction is actually executed.
  107. *)
  108. procedure StackOverrun;
  109. var
  110.   FrontBuffer: array[0..8] of char;
  111.   ToOverrun : array[0..5] of char;
  112.   BackBuffer:array[0..8] of char;
  113. begin
  114.    try
  115.       FrontBuffer[0] := 'a';
  116.       FrontBuffer[1] := 'b';
  117.       FrontBuffer[2] := 'a';
  118.       FrontBuffer[3] := 'b';
  119.       FrontBuffer[4] := 'a';
  120.       FrontBuffer[5] := 'b';
  121.       FrontBuffer[6] := 'a';
  122.       FrontBuffer[7] := 'b';
  123.       FrontBuffer[8] := 'a';
  124.       StrCopy ( ToOverrun, 'Overrun');
  125.       BackBuffer[0] := 'a';
  126.       BackBuffer[1] := 'b';
  127.       BackBuffer[2] := 'a';
  128.       BackBuffer[3] := 'b';
  129.       BackBuffer[4] := 'a';
  130.       BackBuffer[5] := 'b';
  131.       BackBuffer[6] := 'a';
  132.       BackBuffer[7] := 'b';
  133.       BackBuffer[8] := 'a';
  134.    except
  135.    end;
  136. end;
  137.  
  138. (*
  139.    Overrun a stack variable using the FillChar routine.  Simply copying
  140.    too many bytes to the stack variable.
  141. *)
  142. procedure StackOverrun_FillChar;
  143. var
  144.   FrontBuffer: array[0..8] of char;
  145.    ToOverrun :array[0..5] of char;
  146.   BackBuffer:array[0..8] of char;
  147. begin
  148.    try
  149.       FrontBuffer[0] := 'a';
  150.       FrontBuffer[1] := 'b';
  151.       FrontBuffer[2] := 'a';
  152.       FrontBuffer[3] := 'b';
  153.       FrontBuffer[4] := 'a';
  154.       FrontBuffer[5] := 'b';
  155.       FrontBuffer[6] := 'a';
  156.       FrontBuffer[7] := 'b';
  157.       FrontBuffer[8] := 'a';
  158.       FillChar ( ToOverrun, 7, ' ');
  159.       BackBuffer[0] := 'a';
  160.       BackBuffer[1] := 'b';
  161.       BackBuffer[2] := 'a';
  162.       BackBuffer[3] := 'b';
  163.       BackBuffer[4] := 'a';
  164.       BackBuffer[5] := 'b';
  165.       BackBuffer[6] := 'a';
  166.       BackBuffer[7] := 'b';
  167.       BackBuffer[8] := 'a';
  168.    except
  169.    end;
  170. end;
  171.  
  172. procedure Overrun_Record_Element;
  173. type
  174.   recordtype = Record
  175.              Element1 :string[4];
  176.              Element2 :array[0..4] of char;
  177.              Element3 :DWORD;
  178.              Element4 :string;
  179.    end;
  180. var
  181.    myrec : recordtype;
  182. begin
  183.    try
  184.       StrLCopy ( PChar(Addr(myrec.Element1)), 'Overrun', 6);
  185.    except
  186.    end;
  187. end;
  188.  
  189. procedure Ptr_Refs_Unlocked_Block;
  190. var
  191.    Flip: HGLOBAL;
  192.    Flap: PChar;
  193. begin
  194.    try
  195.       Flip := GlobalAlloc ( GHND, $100 );
  196.       Flap := GlobalLock ( Flip );
  197.       GlobalUnlock ( Flip );
  198.       StrCopy ( Flap, 'Block is unlocked');
  199.       GlobalFree ( Flip );
  200.    except
  201.    end;
  202. end;
  203.  
  204.  
  205. procedure Use_Block_After_Free;
  206. var
  207.    String1 : Pointer;
  208.    Destination : array[0..5] of char;
  209. begin
  210.    try
  211.       GetMem ( String1, 10 );
  212.       FreeMem ( String1 );
  213.       StrCopy ( Destination, String1 );
  214.    except
  215.    end;
  216. end;
  217.