Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object inside Record & Memory Leak #2

Open
hafedh-trimeche opened this issue Jan 4, 2021 · 0 comments
Open

Object inside Record & Memory Leak #2

hafedh-trimeche opened this issue Jan 4, 2021 · 0 comments

Comments

@hafedh-trimeche
Copy link

Hello,

When an Object is embedded into a record, a Memory Leak is generated.

unit Main;
{$I Defines}
interface

uses
  Winapi.Windows, REST.JSON, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
  uDSON;
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    ObjectBtn: TButton;
    procedure ObjectBtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TRecordObject=class
  private
    FP1 : Integer;
    FP2 : TBytes;
    FP3 : string;
    FP4 : TDateTime;
  published
    property P1 : Integer   read FP1 write FP1;
    property P2 : TBytes    read FP2 write FP2;
    property P3 : string    read FP3 write FP3;
    property P4 : TDateTime read FP4 write FP4;
  end;

  TJsonRecord=
  record
    Dummy : string;
    O     : TRecordObject;
  end;


var
  Form1: TForm1;

implementation
{$R *.dfm}

procedure TForm1.ObjectBtnClick(Sender: TObject);
var
  O          : TRecordObject;
  JsonRecord : TJsonRecord;
  sJson      : string;
begin
  O     := TRecordObject.create;
  O.P1  := 11;
  O.P2  := [3,2,1];
  O.P3  := 'String';
  O.P4  := now;
  //////////////////////////////
  JsonRecord       := default(TJsonRecord);
  JsonRecord.Dummy := 'Dummy';
  JsonRecord.O     := O;
  //////////////////////////////
  sJson := TDSON.ToJson(JsonRecord);
  Memo1.Lines.Add(sJson);

  JsonRecord := TDSON.FromJson<TJsonRecord>(sJson);
  sJson      := TDSON.ToJson(JsonRecord);
  Memo1.Lines.Add(sJson);

  FreeAndNil(O);
end;

initialization
  ReportMemoryLeaksOnShutdown := True;
finalization
end.
--------------------------------2021/1/4 16:43:53--------------------------------
A memory block has been leaked. The size is: 36

This block was allocated by thread 0x1220, and the stack trace (return addresses) at the time was:
00645E0F [uMemory.pas][uMemory][NewAllocMem][73]
0040708E [System.pas][System][@GetMem][4830]
00409FF3 [System.pas][System][@NewUnicodeString][25285]
0040A224 [System.pas][System][@UStrFromPWCharLen][25963]
0064F87B [System.JSON.pas][System.JSON][TJSONByteReader.FlushString][1542]
006525DF [System.JSON.pas][System.JSON][TJSONObject.ParseString][2804]
00651BCC [System.JSON.pas][System.JSON][TJSONObject.ParseValue][2544]
006519AA [System.JSON.pas][System.JSON][TJSONObject.ParsePair][2489]
006517C3 [System.JSON.pas][System.JSON][TJSONObject.Parse][2438]
0065190E [System.JSON.pas][System.JSON][TJSONObject.ParseObject][2468]
00651BEC [System.JSON.pas][System.JSON][TJSONObject.ParseValue][2558]

The block is currently used for an object of class: Unknown

The allocation number is: 1575

--------------------------------2021/1/4 16:43:53--------------------------------
A memory block has been leaked. The size is: 36

This block was allocated by thread 0x1220, and the stack trace (return addresses) at the time was:
00645E0F [uMemory.pas][uMemory][NewAllocMem][73]
004070FD [System.pas][System][@ReallocMem][5022]
0040708E [System.pas][System][@GetMem][4830]
0040801B [System.pas][System][TObject.NewInstance][17514]
004087EA [System.pas][System][@ClassCreate][18867]
00408100 [System.pas][System][TObject.Create][17573]
00467EDE [System.Rtti.pas][System.Rtti][PutArg][8024]
00467DB8 [System.Rtti.pas][System.Rtti][RawInvoke][7914]
004681BC [System.Rtti.pas][System.Rtti][Invoke][8136]
0045EF72 [System.Rtti.pas][System.Rtti][TRttiInstanceMethodEx.DispatchInvoke][6011]
004686E7 [System.Rtti.pas][System.Rtti][TRttiMethod.Invoke][9436]

The block is currently used for an object of class: Unknown

The allocation number is: 1893

--------------------------------2021/1/4 16:43:53--------------------------------
A memory block has been leaked. The size is: 20

This block was allocated by thread 0x1220, and the stack trace (return addresses) at the time was:
00645E0F [uMemory.pas][uMemory][NewAllocMem][73]
004070FD [System.pas][System][@ReallocMem][5022]
0040C51D [System.pas][System][DynArraySetLength][36046]
0040B367 [System.pas][System][@InitializeArray][32446]
00452139 [System.Rtti.pas][System.Rtti][MakeDynamic][2752]
0040B3A9 [System.pas][System][@FinalizeRecord][32627]
0045236E [System.Rtti.pas][System.Rtti][TValue.FromArray][2800]
0065B355 [uDSON.pas][uDSON][TDSONValueReader.readDynArrayValue][307]
0065BC22 [uDSON.pas][uDSON][TDSONValueReader.tryReadValueFromJson][443]
0065B9C1 [uDSON.pas][uDSON][TDSONValueReader.setObjValue][414]
0046034B [System.Rtti.pas][System.Rtti][TRttiInstanceProperty.GetName][6478]

The block is currently used for an object of class: Unknown

The allocation number is: 1916

--------------------------------2021/1/4 16:43:53--------------------------------
This application has leaked memory. The small block leaks are (excluding expected leaks registered by pointer):

13 - 20 bytes: Unknown x 1
21 - 36 bytes: Unknown x 2

Note: Memory leak detail is logged to a text file in the same folder as this application. To disable this memory leak check, undefine "EnableMemoryLeakReporting".

Best regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant