* * *

Author Topic: Memory Leak list  (Read 350 times)

Phoenix

  • Newbie
  • Posts: 4
Memory Leak list
« on: January 11, 2017, 12:46:56 pm »
Hello!
I have a problem with this source..
more button click = more memory leak  %)
(tested on delphi berlin starter and work ok)
Windows 10 Home 64bit
Lazarus 1.6.2 (2016-11-12) 64bit

Code: Pascal  [Select]
  1. Type
  2.  PTestRecord = ^TTestRecord;
  3.  TTestRecord = record
  4.   n: Integer;
  5.   s: String[255];
  6.  end;
  7.  
  8.  TTestList = class(TList)
  9.   private
  10.  
  11.   public
  12.    destructor Destroy; override;
  13.  
  14.    function AddEx(n_: Integer; s_: String): Integer;
  15.  
  16.  end;
  17.  
  18.  destructor TTestList.Destroy;
  19.  Var
  20.   i: Integer;
  21.  begin
  22.   for i:= 0 to Count-1 do
  23.   begin
  24.    FreeMem(Items[i],SizeOf(TTestRecord));//Dispose(PTestRecord(Items[i]));
  25.    Items[i]:= nil;
  26.   end;
  27.   Clear;
  28.  
  29.   inherited Destroy;
  30.  end;
  31.  
  32.  function TTestList.AddEx(n_: Integer; s_: String): Integer;
  33.  Var
  34.   p: PTestRecord;
  35.  begin
  36.   GetMem(p,SizeOf(TTestRecord));//New(p);
  37.   with p^ do
  38.   begin
  39.    n:= n_;
  40.    s:= s_;
  41.   end;
  42.   Result:= Add(p);
  43.  end;
  44.  
  45. procedure TForm1.Button1Click(Sender: TObject);
  46. Var
  47.  i: Integer;
  48.  tl: TTestList;
  49. begin
  50.  tl:= TTestList.Create;
  51.  try
  52.   for i:= 1 to 1000000 do
  53.    tl.AddEx(i,'Str'+IntToStr(i));
  54.  
  55.   with PTestRecord(tl[3])^ do
  56.    ShowMessage(IntToStr(n)+','+s);
  57.  finally
  58.   tl.Free;
  59.  end;
  60. end;
  61.  
       
thanks for the help!!
« Last Edit: January 11, 2017, 01:00:57 pm by Phoenix »

Thaddy

  • Hero Member
  • *****
  • Posts: 2455
Re: Memory Leak list
« Reply #1 on: January 11, 2017, 01:13:37 pm »
Did you already try releasing the memory in proper order?
Code: Pascal  [Select]
  1.  destructor TTestList.Destroy;
  2.  Var
  3.   i: Integer;
  4.  begin
  5.   for i:= Count-1 downto 0 do
  6.   begin
  7.    FreeMem(Items[i],SizeOf(TTestRecord));//Dispose(PTestRecord(Items[i]));
  8.    Items[i]:= nil;
  9.   end;
  10.   Clear;
  11.  
  12.   inherited Destroy;
  13.  end;
I did not test it...
 

GetMem

  • Hero Member
  • *****
  • Posts: 1752
Re: Memory Leak list
« Reply #2 on: January 11, 2017, 01:16:45 pm »
I tested your application, pressed the button several times but there is no leak. My configuration:
- win 7(64 bit)
- Lazarus trunk(32 bit)
- FPC 3.0.0

Bart

  • Hero Member
  • *****
  • Posts: 2522
    • Bart en Mariska's Webstek
Re: Memory Leak list
« Reply #3 on: January 11, 2017, 01:21:52 pm »
Your .lpi file is empty!
I recreated it.
HeapTrace reports no memory leaks (tested win32 and win64).

Why do you think there is a memory leak?

Note: although you free the memory (which is correct), this does not mean that the OS "gets this memory back", at leat not instantaneously, so the memory footprint of the program (if you look at it in taskmanager) may grow with each click on Button1.

I added project1.zip whic contains a vaild .lpi with buildmodes for win32 and win64, bith with heaptrace enabled.
Here's part of HepTrace output.

Code: [Select]
C:\Users\Bart\LazarusProjecten\bugs\Forum\TList>project1
Heap dump by heaptrc unit
41535 memory blocks allocated : 5989013/6093760
41535 memory blocks freed     : 5989013/6093760
0 unfreed memory blocks : 0

Bart

Phoenix

  • Newbie
  • Posts: 4
Re: Memory Leak list
« Reply #4 on: January 11, 2017, 01:59:43 pm »
Increased ram only during debug in lazarus (on windows taskmanager);
while running the application no problem.
Thanks Bart for your Note.
Problem solved: thank you all!

Thaddy

  • Hero Member
  • *****
  • Posts: 2455
Re: Memory Leak list
« Reply #5 on: January 11, 2017, 02:03:10 pm »
If it is really a problem, use my code and the memory also decreases while watching... ;)
Release memory in opposite order.

By now I tested it ;)

Phoenix

  • Newbie
  • Posts: 4
Re: Memory Leak list
« Reply #6 on: January 11, 2017, 02:55:30 pm »
Thanks Thaddy,
ram increases much less!!  :)

ASerge

  • Full Member
  • ***
  • Posts: 123
Re: Memory Leak list
« Reply #7 on: January 11, 2017, 05:02:18 pm »
Phoenix, instead of the Destroy sleeker to override Notify. In this case, you not only get the same result (no memory leaks, and release in reverse order, as indicated by Thaddy), but also the ability to delete individual items without memory leaks.
Code: Pascal  [Select]
  1. procedure TTestList.Notify(Ptr: Pointer; Action: TListNotification);
  2. begin
  3.   if Action = lnDeleted then
  4.     Dispose(PTestRecord(Ptr));
  5. end;
Additionally, better use New/Dispose. For example, If you change later string[255] to string, then will be memory leaks for GetMem/FreeMem, but not for New/Dispose.

Phoenix

  • Newbie
  • Posts: 4
Re: Memory Leak list
« Reply #8 on: January 11, 2017, 08:53:42 pm »
ASerge thanks for your solution!!  :)

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus