Recent

Author Topic: Access Violation TPQConnection := False  (Read 1674 times)

LzMan

  • New member
  • *
  • Posts: 8
Access Violation TPQConnection := False
« on: March 03, 2024, 11:23:10 am »
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?

Чебурашка

  • Hero Member
  • *****
  • Posts: 579
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Access Violation TPQConnection := False
« Reply #1 on: March 03, 2024, 11:58:33 am »
Could you please share some code example that allows to reproduce the error?
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

dseligo

  • Hero Member
  • *****
  • Posts: 1247
Re: Access Violation TPQConnection := False
« Reply #2 on: March 03, 2024, 12:17:00 pm »
I am using Lazarus 3.0 with FPC 3.2.2 (latest download).

Latest is Lazarus 3.2, not 3.0.

Quote
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.

This code can't cause access violation, because this code can't compile. Show the real code you use.

LzMan

  • New member
  • *
  • Posts: 8
Re: Access Violation TPQConnection := False
« Reply #3 on: March 03, 2024, 05:31:21 pm »
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][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, PQConnection, SQLDB, DB, Forms, Controls, Graphics,
  9.   Dialogs, DBGrids;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     DataSource1: TDataSource;
  17.     DBGrid1: TDBGrid;
  18.     PQConnection1: TPQConnection;
  19.     SQLQuery1: TSQLQuery;
  20.     SQLTransaction1: TSQLTransaction;
  21.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  22.     procedure FormCreate(Sender: TObject);
  23.   private
  24.  
  25.   public
  26.  
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.FormCreate(Sender: TObject);
  39. begin
  40.   PQConnection1.Connected := True;
  41.   SQLTransaction1.Active := True;
  42.   SQLQuery1.Active := True;
  43. end;
  44.  
  45. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  46. begin
  47.   SQLQuery1.Active := False;
  48.   SQLTransaction1.Active := False;
  49.   ShowMessage('trans close');
  50.   PQConnection1.Connected := False;
  51.   ShowMessage('disconnected');
  52. end;
  53.  
  54. end.
  55.  

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][+][-]
  1.   PQConnection1.Connected := False;
  2.  

LzMan

  • New member
  • *
  • Posts: 8
Re: Access Violation TPQConnection := False
« Reply #4 on: March 03, 2024, 05:33:45 pm »
PS: I am using libpq v13.14

dseligo

  • Hero Member
  • *****
  • Posts: 1247
Re: Access Violation TPQConnection := False
« Reply #5 on: March 03, 2024, 11:46:32 pm »
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.

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

  • New member
  • *
  • Posts: 8
Re: Access Violation TPQConnection := False
« Reply #6 on: March 04, 2024, 12:13:39 am »
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][+][-]
  1. #0  at :-1
  2. #1 libcrypto-1_1-x64:EVP_CIPHER_CTX_buf_noconst+1036834 at :0
  3. #2 ucrtbase:__sys_nerr+691 at :0
  4. #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

  • Hero Member
  • *****
  • Posts: 1247
Re: Access Violation TPQConnection := False
« Reply #7 on: March 04, 2024, 12:54:52 am »
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?

PostgreSQL is 14.4, on Win 11 (same as client).
DLL is 12.1 version.

LzMan

  • New member
  • *
  • Posts: 8
Re: Access Violation TPQConnection := False
« Reply #8 on: March 04, 2024, 12:27:53 pm »
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

  • Hero Member
  • *****
  • Posts: 2374
Re: Access Violation TPQConnection := False
« Reply #9 on: March 04, 2024, 12:49:59 pm »
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)
« Last Edit: March 04, 2024, 12:51:37 pm by Zvoni »
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

Чебурашка

  • Hero Member
  • *****
  • Posts: 579
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Access Violation TPQConnection := False
« Reply #10 on: March 04, 2024, 01:19:23 pm »
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)

We miss the lfm, prob everything is there.
« Last Edit: March 04, 2024, 01:21:15 pm by Чебурашка »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Zvoni

  • Hero Member
  • *****
  • Posts: 2374
Re: Access Violation TPQConnection := False
« Reply #11 on: March 04, 2024, 01:32:15 pm »
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)

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
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

Чебурашка

  • Hero Member
  • *****
  • Posts: 579
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Access Violation TPQConnection := False
« Reply #12 on: March 04, 2024, 01:52:37 pm »
Arrgghh....

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][+][-]
  1. var
  2.   connection: TPQConnection;
  3.   transaction: TSQLTransaction;
  4.   query: TSQLQuery;
  5. begin
  6.   connection := TPQConnection.Create(nil);
  7.   try
  8.      connection.Params...
  9.      connection.Connected := True
  10.      transaction := TSQLTransaction.Create();
  11.      transaction.Connection := connection;
  12.      
  13.      try
  14.        query := TSQLQuery.Create();
  15.        query.Connection := connection;
  16.        query.Transaction := transaction;
  17.        try
  18.          query.SQL.Add('');
  19.          query.Params....
  20.          
  21.          try
  22.            transaction.StartTransaction();
  23.  
  24.            // for queries
  25.            query.Open
  26.            while not query.EOF do
  27.            begin
  28.              // some operation of the results...
  29.  
  30.              query.Next
  31.            end;
  32.            query.Close
  33.            
  34.            // for commands
  35.            //query.ExecSQL
  36.  
  37.            transaction.Commit();
  38.           except
  39.             transaction.Rollback();
  40.           end;
  41.  
  42.  
  43.        finally
  44.          query.Free();
  45.        end;
  46.      finally
  47.        transaction.Free();
  48.      end;
  49.  
  50.   finally
  51.     connection.Free();
  52.   end;
  53. end;
  54.  
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

LzMan

  • New member
  • *
  • Posts: 8
Re: Access Violation TPQConnection := False
« Reply #13 on: March 04, 2024, 02:29:52 pm »
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

  • Hero Member
  • *****
  • Posts: 6206
Re: Access Violation TPQConnection := False
« Reply #14 on: March 04, 2024, 02:33:10 pm »
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.

 

TinyPortal © 2005-2018