Recent

Author Topic: heaptrc and sqlite  (Read 743 times)

ancamini

  • New Member
  • *
  • Posts: 13
heaptrc and sqlite
« on: October 27, 2022, 02:52:47 pm »
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

  • Hero Member
  • *****
  • Posts: 2319
Re: heaptrc and sqlite
« Reply #1 on: October 27, 2022, 03:13:08 pm »
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
I'm gazing into my crystal ball, but i don't see any code there, either....
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

ancamini

  • New Member
  • *
  • Posts: 13
Re: heaptrc and sqlite
« Reply #2 on: October 27, 2022, 05:39:58 pm »
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][+][-]
  1. function THBConfigDb.OpenDb: boolean;
  2. begin
  3.   try
  4.     if not Assigned(Fcnx) then
  5.     begin
  6.       Fcnx := TSQLite3Connection.Create(nil);
  7.       Fcnx.Transaction := TSQLTransaction.Create(Fcnx);
  8.     end
  9.     else
  10.     if Fcnx.Connected then Fcnx.Close();
  11.  
  12.     if not Assigned(Fquery) then
  13.     begin
  14.       Fquery := TSQLQuery.Create(nil);
  15.       Fquery.SQLConnection := Fcnx;
  16.     end
  17.     else if Fquery.Active then Fquery.Close;
  18.  
  19.     Fcnx.DatabaseName := FdirDb + FdbName;
  20.     Fcnx.Open;
  21.     Fcnx.Close();
  22.     Result := True;
  23.     FdbOk := True;
  24.   except
  25.     Result := False;
  26.     FdbOk := False;
  27.     resetQry;
  28.     resetCnx;
  29.   end;
  30. end;                                          
  31.  

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

  • Hero Member
  • *****
  • Posts: 2319
Re: heaptrc and sqlite
« Reply #3 on: October 27, 2022, 05:56:23 pm »
When you exit/terminate your app, do you „Free“ (hint) the created objects (connection, transaction, query)?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: heaptrc and sqlite
« Reply #4 on: October 27, 2022, 06:01:06 pm »
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.
Specialize a type, not a var.

ancamini

  • New Member
  • *
  • Posts: 13
Re: heaptrc and sqlite
« Reply #5 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..

Code: Pascal  [Select][+][-]
  1. constructor THBConfigDb.Create(dbSnsPathName, dirBck, dbElemName: string);
  2. var
  3.   strSql: TStringList;
  4. begin
  5.   inherited Create;
  6.   FdirDb := ExtractFileDir(dbSnsPathName) + PathDelim;
  7.   FdbName := ExtractFileName(dbSnsPathName);
  8.   FdirBck := dirBck + PathDelim;
  9.   FdbElem := dbElemName;
  10.   Fcnx := nil;
  11.   Fquery := nil;
  12.   FdbOk := False;
  13.  
  14.   strSql := TStringList.Create;
  15.   strSql.Add('create table moduli (id INTEGER PRIMARY KEY AUTOINCREMENT,');
  16.   strSql.Add('fam INTEGER, tipo INTEGER, numod INTEGER, etic VARCHAR (30), checked INTEGER,');
  17.   strSql.Add('data VARCHAR (255));');
  18.   FsqlTabMod := strSql.Text;
  19.  
  20.   strSql.Clear;
  21.   strSql.Add('CREATE TABLE componenti (id INTEGER PRIMARY KEY ASC AUTOINCREMENT, idmod INTEGER,');
  22.   strSql.Add('fam INTEGER, tipo INTEGER, numod INTEGER, nuitem INTEGER, etic VARCHAR (30),');
  23.   strSql.Add('data VARCHAR (255));');
  24.   FsqlTabComp := strSql.Text;
  25.   FreeAndNil(strSql);
  26. end;
  27.  
  28.  

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][+][-]
  1. rocedure THBConfigDb.resetCnx;
  2. begin
  3.   try
  4.     if Assigned(Fcnx) then
  5.       if Fcnx.Connected then Fcnx.Close();
  6.     FreeAndNil(Fcnx);
  7.   except
  8.     Fcnx := nil;
  9.   end;
  10. end;                          
  11.  

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

  • Hero Member
  • *****
  • Posts: 6112
Re: heaptrc and sqlite
« Reply #6 on: October 27, 2022, 09:34:32 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

  • New Member
  • *
  • Posts: 13
Re: heaptrc and sqlite
« Reply #7 on: October 27, 2022, 09:57:14 pm »
RVK I agree with you...just now I am cutting away all the "superfluos" code trying to be as small but effective possible...

Carlo

 

TinyPortal © 2005-2018