Recent

Author Topic: Memory Leak. Missing 64 blocks  (Read 743 times)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 792
    • Blog personal
Memory Leak. Missing 64 blocks
« on: January 31, 2023, 10:38:33 am »
Hello, I have the following code:
Code: [Select]
var
  Cadena, NombrePropiedad: string;
  J, I: integer;
  PP: PPropList;
  PPI: Pointer;
  PT: PTypeData;
  TipoPropiedad: TTypeKind;
  b: boolean;
begin
  Cadena := '';

  PPI := aItem.ClassInfo;

  PT := GetTypeData(PPI);
  GetMem(PP, PT^.PropCount * SizeOf(PP));
  J := GetPropList(aItem.ClassType, PP);

  //for I := 0 to J - 1 do
  //begin
  //  with PP^[I]^ do
  //  begin
  //    NombrePropiedad := Name;
  //  end;
  //  TipoPropiedad := PropType(aItem, NombrePropiedad);
  //  case TipoPropiedad of
  //    tkAString:
  //    begin
  //      Cadena := GetStrProp(aItem, NombrePropiedad);
  //      if Cadena <> '' then
  //      begin
  //        Cadena := Format('-%s "%s"', [NombrePropiedad, Cadena]);
  //        aLineas.Add(Cadena);
  //      end;
  //    end;
  //    tkBool:
  //    begin
  //      B := GetPropValue(aItem, NombrePropiedad);
  //      if b then
  //      begin
  //        Cadena := Format('-%s ', [NombrePropiedad]);
  //        aLineas.Add(Cadena);
  //      end;
  //    end;
  //  end;
  //end;
  Freememory(PP, PT^.PropCount * SizeOf(PP)); 
                                   
As you can see I comment many code, but I couln't find where is the leak memory.

Any idea

Thanks in advance

/BlueIcaro

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Memory Leak. Missing 64 blocks
« Reply #1 on: January 31, 2023, 10:41:02 am »
It is FreeMem, NOT FreeMemory.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

balazsszekely

  • Guest
Re: Memory Leak. Missing 64 blocks
« Reply #2 on: January 31, 2023, 11:08:34 am »
@Thaddy
Freememory is just an alias for FreeMem.

@BlueIcaro
Your leak originates from Button2Click. Can you post the code? Even better a small demo that shows the issue would be nice.

BlueIcaro

  • Hero Member
  • *****
  • Posts: 792
    • Blog personal
Re: Memory Leak. Missing 64 blocks
« Reply #3 on: January 31, 2023, 11:16:29 am »
@Thaddy
Freememory is just an alias for FreeMem.

@BlueIcaro
Your leak originates from Button2Click. Can you post the code? Even better a small demo that shows the issue would be nice.

With Freemem I got the same problem because is the same instruccion as you say GetMem

Here is the buttonClick2 Code:
Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
var
  E: TCrossConexionList;
  Lista, L: TStringList;
  I: integer;
begin
  if OpenDialog1.Execute then
  begin
    Lista := TStringList.Create;
    Lista.LoadFromFile(OpenDialog1.FileName);
    E := TCrossConexionList.Create;
    E.LoadFromStrings(Lista);
    Memo1.Lines.Clear;
    L := E[0].GenerateLines;
    FreeAndNil(L);
    FreeAndNil(Lista);
    FreeAndNil(E);
  end;
end;
Also a attach all project.
/BlueIcaro

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Memory Leak. Missing 64 blocks
« Reply #4 on: January 31, 2023, 11:18:19 am »
Try code such as this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ShowItemInfo(aItem: TComponent; aLineas: TMemo);
  2. var
  3.   Cadena, NombrePropiedad: String;
  4.   propCount, i: Integer;
  5.   PropListPtr: PPropList;
  6.   TipoPropiedad: TTypeKind;
  7.  
  8. begin
  9.   Cadena := '';
  10.   propCount := GetPropList(aItem.ClassType, PropListPtr);
  11.   if propCount > 0 then
  12.     try
  13.     for i := 0 to propCount - 1 do
  14.       begin
  15.         NombrePropiedad := PropListPtr^[I]^.Name;
  16.         TipoPropiedad := PropType(aItem, NombrePropiedad);
  17.         case TipoPropiedad of
  18.           tkAString: begin
  19.                         Cadena := GetStrProp(aItem, NombrePropiedad);
  20.                         if Cadena <> '' then
  21.                           aLineas.Lines.Add('-%s "%s"', [NombrePropiedad, Cadena]);
  22.                       end;
  23.           tkBool: if {%H-}GetPropValue(aItem, NombrePropiedad) then
  24.                     aLineas.Lines.Add('-%s ', [NombrePropiedad]);
  25.         end;
  26.       end;
  27.     finally
  28.       Freemem(PropListPtr);
  29.     end;
  30. end;
You don't need an explicit GetMem, since the call to GetPropList allocates the memory required.
« Last Edit: January 31, 2023, 11:24:26 am by howardpc »

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Memory Leak. Missing 64 blocks
« Reply #5 on: January 31, 2023, 02:29:33 pm »
Indeed. It is also the cause of the leak.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

BlueIcaro

  • Hero Member
  • *****
  • Posts: 792
    • Blog personal
Re: Memory Leak. Missing 64 blocks
« Reply #6 on: February 01, 2023, 08:46:13 am »
Thanks howardPC, now I don't have a memory Leak. By the way the original code is from pascal help file. May be should be update

https://www.freepascal.org/docs-html/rtl/typinfo/getproplist.html

/BlueIcaro

 

TinyPortal © 2005-2018