#1 2020-04-08 17:35:06

sakura
Member
From: Germany
Registered: 2018-02-21
Posts: 218
Website

Memory Leak, when Rest Server Create with DB

Hi,

last post was a side effect of my actual problem. I have a memory leak (Delphi 10.3.3), and finally, I have reproduced it with minimal code.

program mORMot.bug;

{$APPTYPE CONSOLE}

{$R *.res}

uses
//  FastMM4,
  System.SysUtils,
  mORMot,
  mORMotDDD,
  mORMotSQLite3,
  SynCommons,
  SynSQLite3,
  SynSQLite3Static;

type
  TSQLTest = class(TSQLRecord)
  private
    FPassword: RawUTF8;
    FLogin: RawUTF8;
    FSalt: RawUTF8;
  published
    property Login: RawUTF8 read FLogin write FLogin;
    property Password: RawUTF8 read FPassword write FPassword;
    property Salt: RawUTF8 read FSalt write FSalt;
  end;

var
  Model: TSQLModel;
  DB: TSQLDatabase;
  Rest: TSQLRestServerDB;
  Test: TSQLTest;
  Success: Boolean;
begin
  ReportMemoryLeaksOnShutdown := True;

  try
    Model := TSQLModel.Create([TSQLTest]);
    DB := TSQLDatabase.Create('2.db');
    Rest := TSQLRestServerDB.Create(Model, DB);
    Rest.CreateMissingTables;

    Test := TSQLTest.Create();
    try
      Success := Rest.Retrieve('Login = ?', [], ['youOrMe'], Test);

      Writeln('Success: ', Success);
    finally
      Test.Free;
    end;

    DB.Free;
    Rest.Free;
    Model.Free;

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;

  Write('Done ');
  Readln;
end.

Adding FastMM to the uses, further reproduces the problem from my other current topic.

Memory Leak Report

Unexpected Memory Leak
An unexpected memory leak has occurred. The unexpected small block leaks are:

13 - 20 bytes: Unknown x 31
21 - 28 bytes: Unknown x 90
29 - 36 bytes: Unknown x 1
37 - 44 bytes: Unknown x 15
45 - 52 bytes: Unknown x 2
53 - 60 bytes: Unknown x 28
61 - 68 bytes: Unknown x 12
69 - 76 bytes: Unknown x 4
77 - 84 bytes: Unknown x 2
85 - 92 bytes: Unknown x 6
125 - 132 bytes: Unknown x 1
157 - 172 bytes: Unknown x 5
477 - 524 bytes: Unknown x 1
621 - 668 bytes: Unknown x 2
957 - 1052 bytes: Unknown x 1

The sizes of unexpected leaked medium and large blocks are: 85292, 48172, 4140

Regards,
Daniel

Offline

#2 2020-04-08 17:48:54

Eugene Ilyin
Member
From: milky_way/orion_arm/sun/earth
Registered: 2016-03-27
Posts: 132
Website

Re: Memory Leak, when Rest Server Create with DB

Hi Daniel,

The issues is in construction/destruction sequence:

For the given creation sequence:

CreateModel
  CreateDataBase
    CreateRestServer

It's better to destruct everything in reverse order:

    DestroyRestServer
  DestroyDataBase
DestroyModel

So changing

    DB.Free;
    Rest.Free;

to

    Rest.Free;
    DB.Free;

Will finalyze/destroy/unassign all properties in proper sequence and leaks are gone.

Last edited by Eugene Ilyin (2020-04-09 00:05:51)

Offline

#3 2020-04-08 19:57:34

sakura
Member
From: Germany
Registered: 2018-02-21
Posts: 218
Website

Re: Memory Leak, when Rest Server Create with DB

Ouch, should have seen that myself. Thanks!!!!!

Offline

#4 2020-04-08 20:27:27

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: Memory Leak, when Rest Server Create with DB

Just one more option, if the model will not be shared, you leave the TSQLRestServerDB as Owner, so when destroying the TSQLRestServerDB, the Model is also destroyed.

DB := TSQLDatabase.Create('2.db');
Rest := TSQLRestServerDB.Create(TSQLModel.Create([TSQLTest]), DB);
Rest.Model.Owner = Rest;
try
 ...
finally
   Rest.Free; //The Model are destroyed together
   DB.Free;
end;

Offline

#5 2020-04-09 05:48:04

sakura
Member
From: Germany
Registered: 2018-02-21
Posts: 218
Website

Re: Memory Leak, when Rest Server Create with DB

macfly wrote:

Just one more option, if the model will not be shared, you leave the TSQLRestServerDB as Owner, so when destroying the TSQLRestServerDB, the Model is also destroyed.

As written before, this was a sample, boiled down to minimal code. Interestingly, the same mistake happened in the sample, as in the real app. However, in the sample it was way more obvious.

Offline

Board footer

Powered by FluxBB