Access Violation TPQConnection := False lazarus / 16-Databases ---------------------------------------------------------------------- LzMan · 2024-03-03 10:23 I am using Lazarus 3.0 with FPC 3.2.2 (latest download). I am new to Lazarus and managed to fill a DBGrid with data from a Postgresql database. It is a minimal application, just: - PQConnection - SQLTransaction - SQLQuery - DataSource - DBGrid But when I close the application, I get an access violation. I tracked this further and found that executing PQConnection1 := False; causes the access violation. Interestingly, when I run my application standalone, it does not give an access violation. Any hints? ---------------------------------------------------------------------- Чебурашка · 2024-03-03 10:58 Could you please share some code example that allows to reproduce the error? ---------------------------------------------------------------------- dseligo · 2024-03-03 11:17 Quote from: LzMan on March 03, 2024, 11:23:10 amI am using Lazarus 3.0 with FPC 3.2.2 (latest download). Latest is Lazarus 3.2, not 3.0. QuoteBut when I close the application, I get an access violation. I tracked this further and found that executing PQConnection1 := False; causes the access violation. This code can't cause access violation, because this code can't compile. Show the real code you use. ---------------------------------------------------------------------- LzMan · 2024-03-03 16:31 I don't know how it came that I downloaded Lazarus v3.0. I followed a link from the Lazarus website. Anyway, I now installed v3.2 and the problem persists. Here is my source code. 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 unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, PQConnection, SQLDB, DB, Forms, Controls, Graphics,  Dialogs, DBGrids; type   { TForm1 }   TForm1 = class(TForm)    DataSource1: TDataSource;    DBGrid1: TDBGrid;    PQConnection1: TPQConnection;    SQLQuery1: TSQLQuery;    SQLTransaction1: TSQLTransaction;    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);  procedure FormCreate(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  PQConnection1.Connected := True;  SQLTransaction1.Active := True;  SQLQuery1.Active := True;end; procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);begin  SQLQuery1.Active := False;  SQLTransaction1.Active := False;  ShowMessage('trans close');  PQConnection1.Connected := False;  ShowMessage('disconnected');end; end.  I manually activated / deactivated Query, Transaction, Connection to narrow down where it crashes. As written, it crashes when executing this line of code: 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   PQConnection1.Connected := False; ---------------------------------------------------------------------- LzMan · 2024-03-03 16:33 PS: I am using libpq v13.14 ---------------------------------------------------------------------- dseligo · 2024-03-03 22:46 Quote from: LzMan on March 03, 2024, 05:31:21 pmI don't know how it came that I downloaded Lazarus v3.0. I followed a link from the Lazarus website. Anyway, I now installed v3.2 and the problem persists. I didn't mean to say it has something to do with Lazarus version, only that 3.2 is last version and not 3.0. You code seems fine. I attached test project which doesn't crash, you can try it. It is basically your code, I only added some set-up so you can adapt to your DB installation easier. I only added 5 lines in FormCreate event. ---------------------------------------------------------------------- LzMan · 2024-03-03 23:13 Thanks for your help. Unfortunately your sample also crashes on my machine at the line "PQConnection1 := False" The call stack seems to be corrupted, so no information from this: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i #0  at :-1#1 libcrypto-1_1-x64:EVP_CIPHER_CTX_buf_noconst+1036834 at :0#2 ucrtbase:__sys_nerr+691 at :0#3  at :-1 In my case the Postgres database is on a Linux server, it is version 13.7. Local machine is Win 11 x64. What version of libpq.dll are you using? ---------------------------------------------------------------------- dseligo · 2024-03-03 23:54 Quote from: LzMan on March 04, 2024, 12:13:39 amIn my case the Postgres database is on a Linux server, it is version 13.7. Local machine is Win 11 x64. What version of libpq.dll are you using? PostgreSQL is 14.4, on Win 11 (same as client). DLL is 12.1 version. ---------------------------------------------------------------------- LzMan · 2024-03-04 11:27 I tried libpq v12 and same result. I said it doesn't crash when run standalone, but this is wrong. The exception is just not shown by Windows. I attached the Visual Studio 2022 Debugger to the standalone exe and there is an access violation. Stack trace shows it happens in libcrypto, which belongs to Postgres. Is it possible to debug the code that is executed when PQConnection1 is set to False? ---------------------------------------------------------------------- Zvoni · 2024-03-04 11:49 If that is the whole code, then i'm trying to wrap my mind around the fact, that you connect to a PG-Server without providing 1) a Hostname/IP-Adress of the server 2) A Port-Number 3) A UserName/Password 4) The sqldb-Trinity is not interconnected Nevermind, that i don't see anywhere in your code, that you "create" your Classes And then you complain it crashes when setting Connected to False??? It would surprise me, if the Connection even gets the state of "Connected=True" in the first place. Guess: the "second" access to a Property of the connection causes AV, because it doesn't exist (Null-Pointer) ---------------------------------------------------------------------- Чебурашка · 2024-03-04 12:19 Quote from: Zvoni on March 04, 2024, 12:49:59 pmIf that is the whole code, then i'm trying to wrap my mind around the fact, that you connect to a PG-Server without providing 1) a Hostname/IP-Adress of the server 2) A Port-Number 3) A UserName/Password 4) The sqldb-Trinity is not interconnected Nevermind, that i don't see anywhere in your code, that you "create" your Classes And then you complain it crashes when setting Connected to False??? It would surprise me, if the Connection even gets the state of "Connected=True" in the first place. Guess: the "second" access to a Property of the connection causes AV, because it doesn't exist (Null-Pointer) We miss the lfm, prob everything is there. ---------------------------------------------------------------------- Zvoni · 2024-03-04 12:32 Quote from: Чебурашка on March 04, 2024, 01:19:23 pmQuote from: Zvoni on March 04, 2024, 12:49:59 pmIf that is the whole code, then i'm trying to wrap my mind around the fact, that you connect to a PG- Server without providing 1) a Hostname/IP-Adress of the server 2) A Port-Number 3) A UserName/Password 4) The sqldb-Trinity is not interconnected Nevermind, that i don't see anywhere in your code, that you "create" your Classes And then you complain it crashes when setting Connected to False??? It would surprise me, if the Connection even gets the state of "Connected=True" in the first place. Guess: the "second" access to a Property of the connection causes AV, because it doesn't exist (Null-Pointer) We miss the lfm, prob everything is there. Arrgghh....you mean he used those blasted "visual" components, and set up everything in Object-Inspector? In that case, i'm the wrong guy to ask.... don't use them ---------------------------------------------------------------------- Чебурашка · 2024-03-04 12:52 Quote from: Zvoni on March 04, 2024, 01:32:15 pmArrgghh.... For a newcomer it is good to get familiar with the environment, and also using these components can be good as starting point. Naturally all developers evolve to more advanced ways of doing db connectivity as they improve their knowledge and experience. But maybe the reason of the AV here s related to the usage of the components. If that could help, I also do postgres works and I get no error. Maybe a suggestion to this user could be to try doing the same actions, but without using the db compoenents in the gui designer, but use underlying db obejcts directly, something like: 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 var  connection: TPQConnection;  transaction: TSQLTransaction;  query: TSQLQuery;begin  connection := TPQConnection.Create(nil);  try     connection.Params...     connection.Connected := True     transaction := TSQLTransaction.Create();     transaction.Connection := connection;          try       query := TSQLQuery.Create();       query.Connection := connection;       query.Transaction := transaction;       try         query.SQL.Add('');         query.Params....                  try           transaction.StartTransaction();            // for queries           query.Open           while not query.EOF do           begin           // some operation of the results...              query.Next         end;           query.Close                       // for commands         //query.ExecSQL            transaction.Commit();          except            transaction.Rollback();          end;         finally       query.Free();       end;     finally       transaction.Free();     end;   finally    connection.Free();  end;end; ---------------------------------------------------------------------- LzMan · 2024-03-04 13:29 To the last people writing posts: Earlier the user "dseligo" created a sample that is working for him, and it crashes on my machine. This is documented in this thread. What dseligo did different to me, was to provide the connection params by code. I did set them in the object inspector. So this is not the problem. I used x64dbg and can say that PQfinish() in libpq is called sucessfully, which closes the connection on low level. The AV in libcrypto indicates, that some other function of libpq is called, with the invalidated connection handle. The question is: why does this happen on my machine, but not on the machine of user dseligo? Can anyone give me a pointer how to debug the source code of TPQConnection? ---------------------------------------------------------------------- rvk · 2024-03-04 13:33 I'm not sure what debugger you use. But could you try if you get the same if you use "Run without debugger" in the ide. ---------------------------------------------------------------------- LzMan · 2024-03-04 13:40 Normally, I use the debugger of Lazarus. I wrote in a previous post that the AV also happens when I attach the Visual Studio debugger to the standalone executable. The AV is definitely there. The AV is also there in x64dbg. ---------------------------------------------------------------------- rvk · 2024-03-04 14:07 Quote from: LzMan on March 04, 2024, 02:40:28 pmNormally, I use the debugger of Lazarus.What debugger is that exactly? Lazarus can have several. Gdb or fp-debugger. And depending on the debugger it's also important which debugger options you set. But so it's not the ide itself that's the problem but the attachment of a debugger. I'm not familiar with x64dbg or de debugger from VS so I don't know if there are known issues there. ---------------------------------------------------------------------- LzMan · 2024-03-04 17:25 Can we stay on topic, please? How can I debug the LCL? ---------------------------------------------------------------------- rvk · 2024-03-04 18:09 Quote from: LzMan on March 04, 2024, 06:25:57 pmCan we stay on topic, please? How can I debug the LCL? You would need to rebuild Lazarus with debug option. https://forum.lazarus.freepascal.org/index.php?topic=61774.0 The problem is that you probably don't want LCL because the database function is not from LCL but from FPC. You could build both with debugging on to be able to trace through the source. How did you install FPC and Lazarus? You could try fpcupdeluxe but I'm not sure how to set the debug options (never used fpcupdeluxe). ---------------------------------------------------------------------- Zvoni · 2024-03-04 18:19 Instead of waxing philosophical questions, TS should try a barebone sample-app without „visuals“ (read: simple console app), and check if his AV still appears (my bet is: no, it won’t AV if done correctly) ---------------------------------------------------------------------- jcmontherock · 2024-03-04 22:47 With MySQL i'm using:  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 if Connection1.Connected then Connection1.Close;without problem...