heaptrc and sqlite lazarus / 16-Databases ---------------------------------------------------------------------- ancamini · 2022-10-27 12:52 hi, i have some problems using unit heaptrc with TSQLite3Connection and TSQLQuery. i have written a console program thath uses messages and queues quite extensively and without saving messages in the db I have no leak. When I save data into db I have leaks...records are ok into db, no error while saving... but when program terminates in trace.log i see leaking related to db operations. I have tried to create and open db in several ways...debugging step by step inserting and updating query. All seems ok, but again on program termination i have log with a lot of block leak. Could anyone put me on the right path to solve the problem? Thanks for your time anyway Carlo ---------------------------------------------------------------------- Zvoni · 2022-10-27 13:13 Quote from: ancamini on October 27, 2022, 02:52:47 pmhi, i have some problems using unit heaptrc with TSQLite3Connection and TSQLQuery. i have written a console program thath uses messages and queues quite extensively and without saving messages in the db I have no leak. When I save data into db I have leaks...records are ok into db, no error while saving... but when program terminates in trace.log i see leaking related to db operations. I have tried to create and open db in several ways...debugging step by step inserting and updating query. All seems ok, but again on program termination i have log with a lot of block leak. Could anyone put me on the right path to solve the problem? Thanks for your time anyway Carlo I'm gazing into my crystal ball, but i don't see any code there, either.... ---------------------------------------------------------------------- ancamini · 2022-10-27 15:39 Hi Zvoni...thank you for answering I think it may be useful to attach the trace file. Perhaps for a more experienced person it can provide useful information. I copied the routine, which is indicated in the log on line 263 Call trace for block $00007F8322912680 size 144   $00000000004025F7  INITDBCONFIG,  line 263 of mainrack40.lpr   $000000000040328B  DORUN,  line 471 of mainrack40.lpr   $0000000000486B69 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function THBConfigDb.OpenDb: boolean;begin  try    if not Assigned(Fcnx) then  begin      Fcnx := TSQLite3Connection.Create(nil);      Fcnx.Transaction := TSQLTransaction.Create(Fcnx);    end    else    if Fcnx.Connected then Fcnx.Close();     if not Assigned(Fquery) then    begin      Fquery := TSQLQuery.Create(nil);      Fquery.SQLConnection := Fcnx;    end    else if Fquery.Active then Fquery.Close;     Fcnx.DatabaseName := FdirDb + FdbName;    Fcnx.Open;    Fcnx.Close();    Result := True;    FdbOk := True;  except    Result := False;    FdbOk := False;    resetQry;    resetCnx;  end;end;                                         All the code is in a small class used to save messages read from 2 can bus channels. Messages are pulled from a queue in the main program. I can post all the code but maybe it's better if I put the one mentioned in the log. Carlo p.s. sorry for my english but i got help from a translator ---------------------------------------------------------------------- Zvoni · 2022-10-27 15:56 When you exit/terminate your app, do you „Free“ (hint) the created objects (connection, transaction, query)? ---------------------------------------------------------------------- Thaddy · 2022-10-27 16:01 There are so many things wrong with that code that it makes it hard to debug. I miss a lot of try/finally and you make a lot of assumptions that the first line is always valid, which it isn't... And heaptrc with the proper options (line info etc, and the sourcecode in place) would point you exactly to your leaks. ---------------------------------------------------------------------- ancamini · 2022-10-27 19:19 Thaddy,I am aware that not seeing all the code you may think that the first line is wrong... but before in Create i wrote.. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i constructor THBConfigDb.Create(dbSnsPathName, dirBck, dbElemName: string);var  strSql: TStringList;begin  inherited Create;  FdirDb := ExtractFileDir(dbSnsPathName) + PathDelim;  FdbName := ExtractFileName(dbSnsPathName);  FdirBck := dirBck + PathDelim;  FdbElem := dbElemName;  Fcnx := nil;  Fquery := nil;  FdbOk := False;   strSql := TStringList.Create;  strSql.Add('create table moduli (id INTEGER PRIMARY KEY AUTOINCREMENT,');  strSql.Add('fam INTEGER, tipo INTEGER, numod INTEGER, etic VARCHAR (30), checked INTEGER,');  strSql.Add('data VARCHAR (255));');  FsqlTabMod := strSql.Text;   strSql.Clear;  strSql.Add('CREATE TABLE componenti (id INTEGER PRIMARY KEY ASC AUTOINCREMENT, idmod INTEGER,');  strSql.Add('fam INTEGER, tipo INTEGER, numod INTEGER, nuitem INTEGER, etic VARCHAR (30),');  strSql.Add('data VARCHAR (255));');  FsqlTabComp := strSql.Text;  FreeAndNil(strSql);end;   so I am quite sure that the first line can test that it is assigned or not. In no other place I recreate the connection nor query, but surely there are problems... And about heaptrc the log do write row number and name of the procedure/function, from here I see where to go to see the source code... but debugging step by step i don not see any errors... Zvoni this is the code that terminates connection...same for query Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i rocedure THBConfigDb.resetCnx;begin  try    if Assigned(Fcnx) then      if Fcnx.Connected then Fcnx.Close();    FreeAndNil(Fcnx);  except    Fcnx := nil;  end;end;                           but I am not sure about the code in the except. As for the Transaction Fcnx is the owner so i assume that it is released by Fcnx. Carlo ---------------------------------------------------------------------- rvk · 2022-10-27 19:34 Quote from: ancamini on October 27, 2022, 09:19:48 pm Thaddy,I am aware that not seeing all the code you may think that the first line is wrong... but before in Create i wrote.. Make a small project which reproduces your problem because this is nuts. We see just half of the code. There is no problem with heaptrc, TSQLite3Connection and TSQLQuery. But you have problems in your code. For example you also don't show the freeing of Fquery. You also don't show where you call those 'reset' functions (other then when failing to open). But if you assign Fcnx as the owner you wouldn't even need to free it (Fcnx will do that). When you are stripping your code to get a smaller repoducable example you will probably stumble on your error in thinking yourself. ---------------------------------------------------------------------- ancamini · 2022-10-27 19:57 RVK I agree with you...just now I am cutting away all the "superfluos" code trying to be as small but effective possible... Carlo