You are not logged in.
Hi, I noticed that interface implementation of next code produces memory leaks:
var
locDB: TSQLDBConnectionProperties;
sql: RawUtf8;
rows: ISQLDBRows;
begin
locDB := TSQLDBSQLite3ConnectionProperties.Create(StringToUTF8('someDB.db'),'','','');
try
sql := 'select someColumn from someTable where...';
rows := locDB.Execute(sql,[]);//* produces memory leak
while rows.Step do //* and each step produces memory leak too
begin
(...)
end;
finally
locDB.Free;
end;
end;
and this does not:
var
locDB: TSQLDBConnectionProperties;
sql: RawUtf8;
rows: TSQLDBStatement;
begin
locDB := TSQLDBSQLite3ConnectionProperties.Create(StringToUTF8('someDB.db'),'','','');
try
sql := 'select someColumn from someTable where...';
rows := locDB.NewThreadSafeStatement;
try
rows.Execute(SQL,true);
while rows.Step do
begin
(...)
end;
finally
rows.Free;
end;
finally
locDB.Free;
end;
end;
"Uncertainty in science: There no doubt exist natural laws, but once this fine reason of ours was corrupted, it corrupted everything.", Blaise Pascal
Offline
The TSQLDBConnectionProperties instance should not be released before the ISQLDBRows instance, which is the case in your code.
This is a well known limitation (feature?) of any Delphi interface: the rows.Free is created by the compiler at the end of this method with an hidden try..finally block - see our articles about this.
You should not use a ISQLDBRows interface in such cases, but a regular TSQLDBStatement instance.
In fact, this code is correct:
var
locDB: TSQLDBConnectionProperties;
sql: RawUtf8;
rows: TSQLDBStatement;
begin
locDB := TSQLDBSQLite3ConnectionProperties.Create(StringToUTF8('someDB.db'),'','','');
try
sql := 'select someColumn from someTable where...';
rows := locDB.Execute(sql,[]);//* produces memory leak
try
while rows.Step do //* and each step produces memory leak too
begin
(...)
end;
finally
rows.Free;
end;
finally
locDB.Free;
end;
end;
In the framework, TSQLDBConnectionProperties instances are mostly system-wide so you won't suffer for this.
Offline
Sorry for bothering you, that was my mistake .
I know when the interface goes out of scope.
I didn't really look the source code about relationship between locDB and rows (in this case).
So I didn't realized importance of "destruction" sequence.
"Uncertainty in science: There no doubt exist natural laws, but once this fine reason of ours was corrupted, it corrupted everything.", Blaise Pascal
Offline