You are not logged in.
Pages: 1
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
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
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
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
Pages: 1