#1 2024-12-12 16:17:18

DonAlfredo
Member
Registered: 2023-10-31
Posts: 8

Memory leak when using getters and setters for collection.

A memory leak is occurring when doing the following.

  TResultsRun = class(TCollectionItem)
  private
    fTDC     : TTestDataCollection;
  protected
    function GetNewerTestData:TTestDataCollection;
    procedure SetNewerTestData(aValue:TTestDataCollection);
  public
    constructor Create(ACollection: TCollection); override;
    destructor Destroy;override;
  published
    //property NewerTestData : TTestDataCollection read fTDC write fTDC; <----- all ok !!
    property NewerTestData : TTestDataCollection read GetNewerTestData write SetNewerTestData; <----- memory leak
  end;

(the getter and setter are simple assignments)
Are getters and setters supported for collections ?

Offline

#2 2024-12-12 16:20:36

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,742
Website

Re: Memory leak when using getters and setters for collection.

Do you mean with JSON serialization?

Offline

#3 2024-12-12 16:29:57

DonAlfredo
Member
Registered: 2023-10-31
Posts: 8

Re: Memory leak when using getters and setters for collection.

Yes I guess. This occurs in an interface based client-server setting.

Offline

#4 2024-12-12 17:24:51

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,742
Website

Re: Memory leak when using getters and setters for collection.

You have to write the setter correctly, as such:

procedure TCollTst.SetColl(const Value: TCollTests);
begin
  fColl.Free;
  fColl := Value;
end;

This comes from test.core.data.pas regression tests.

Offline

#5 2024-12-14 13:16:30

DonAlfredo
Member
Registered: 2023-10-31
Posts: 8

Re: Memory leak when using getters and setters for collection.

Thanks Ab ! Solves.

If I may hack this thread for another TCollection question.

In my applications, I use TCollections a lot. They might contain a lot of data. And this data is stored as JSON in the database for external use by non-mORMot applications.
All very well.
However, I would like to consider these TCollection fields as a kind of blobs, due to their huge data-size. So, I want to exclude them when retrieving data from the database.
Code for that (please excuse me for a bit long code listing):

  rA := Rtti.RegisterClass(TOrmProduct);
  pa := pointer(rA.Props.List);
  count:=rA.Props.Count;
  SQL:='';
  repeat
    if pa^.Value.ValueRtlClass=vcCollection then
    begin
      if pa^.Value.ValueClass.ClassParent=TDataRunCollection then SQL:=SQL+','+pa^.Name;
    end;
    inc(pa);
    dec(count);
  until count = 0;
  Delete(SQL,1,1);
  Fields:=TOrmProduct.OrmProps.FieldBitsFromExcludingCsv(SQL);

Question. Or feature request.
Would it be possible to implement something like this ?

Fields:=TOrmProduct.OrmProps.FieldBitsFromExcludingFromDataType(TDataRunCollection);
Fields:=TOrmProduct.OrmProps.FieldBitsFromExcludingFromClassParent(TDataRunCollection);

Offline

#6 2024-12-16 09:52:28

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,742
Website

Re: Memory leak when using getters and setters for collection.

Offline

#7 2024-12-16 10:18:21

DonAlfredo
Member
Registered: 2023-10-31
Posts: 8

Re: Memory leak when using getters and setters for collection.

Works perfect. Thanks !!

Fields:=TOrmProduct.OrmProps.FieldBitsFromExcludingClass([TDataRunCollection],ooSelect,True);

Offline

Board footer

Powered by FluxBB