View Issue Details

IDProjectCategoryView StatusLast Update
0037645FPCDatabasepublic2020-11-24 09:11
ReporterLuiz Americo Assigned ToMichael Van Canneyt  
Status resolvedResolutionfixed 
Product Version3.3.1 
Fixed in Version3.3.1 
Summary0037645: SqlDb: Calling TSQLQuery.Prepare directly, opening, closing and re opening does not work with IBConnection
DescriptionWhen TSQLQuery.Prepare is called directly, query is open, closed and re opened, it does not work with IBConnection.

This use case is tested in TTestTSQLQuery.TestPrepareCount2 from DBTestFramework

Below is a patch that disposes cursor memory on UnPrepareStatement instead of FreeFldBuffers, making the cursor memory allocation balanced between PrepareStatement and UnPrepareStatement. This fixes the access violation, but now fails with another error:

TIBConnection : Fetch :
 -Dynamic SQL Error
 -SQL error code = -502
 -Attempt to reopen an open cursor
Exception class: EIBDatabaseError
at $006B6208 TIBCONNECTION__CHECKERROR, line 195 of D:/repositories/fpc-playground/packages/fcl-db/src/sqldb/interbase/ibconnection.pp

TagsNo tags attached.
Fixed in Revision47400
Attached Files


parent of 0038124 assignedMichael Van Canneyt Regression after r47400: Firebird cannot CREATE or ALTER TABLE anymore 
related to 0031231 resolvedMichael Van Canneyt procedure TCustomSQLQuery.Prepare does not work 
Not all the children of this issue are yet resolved or closed.


Luiz Americo

2020-08-27 01:15


ibconnection-prepare-crash.diff (1,014 bytes)   
diff --git packages/fcl-db/src/sqldb/interbase/ibconnection.pp packages/fcl-db/src/sqldb/interbase/ibconnection.pp
index 51127ab83..7926f2b4c 100644
--- packages/fcl-db/src/sqldb/interbase/ibconnection.pp
+++ packages/fcl-db/src/sqldb/interbase/ibconnection.pp
@@ -899,6 +899,7 @@ procedure TIBConnection.UnPrepareStatement(cursor : TSQLCursor);
   with cursor as TIBcursor do
+  begin
     if assigned(StatementHandle) Then
         if isc_dsql_free_statement(@Status[0], @StatementHandle, DSQL_Drop) <> 0 then
@@ -906,6 +907,9 @@ begin
         StatementHandle := nil;
         FPrepared := False;
+    FreeSQLDABuffer(SQLDA);
+    FreeSQLDABuffer(in_SQLDA);
+  end;
 procedure TIBConnection.FreeSQLDABuffer(var aSQLDA : PXSQLDA);
@@ -949,8 +953,6 @@ procedure TIBConnection.FreeFldBuffers(cursor : TSQLCursor);
   with cursor as TIBCursor do
-    FreeSQLDABuffer(SQLDA);
-    FreeSQLDABuffer(in_SQLDA);

Michael Van Canneyt

2020-11-12 15:01

administrator   ~0126862

Fixed. Your patch was partially correct.

Turns out you need to create a cursor within firebird when using prepared queries.
We never noticed because we closed the statement when closing the dataset prior to the prepare fixes...

Thanks for reporting and for the patch.

Issue History

Date Modified Username Field Change
2020-08-27 01:15 Luiz Americo New Issue
2020-08-27 01:15 Luiz Americo File Added: ibconnection-prepare-crash.diff
2020-08-27 07:07 Michael Van Canneyt Assigned To => Michael Van Canneyt
2020-08-27 07:07 Michael Van Canneyt Status new => assigned
2020-11-12 15:01 Michael Van Canneyt Status assigned => resolved
2020-11-12 15:01 Michael Van Canneyt Resolution open => fixed
2020-11-12 15:01 Michael Van Canneyt Fixed in Version => 3.3.1
2020-11-12 15:01 Michael Van Canneyt Fixed in Revision => 47400
2020-11-12 15:01 Michael Van Canneyt FPCTarget => 3.2.2
2020-11-12 15:01 Michael Van Canneyt Note Added: 0126862
2020-11-13 23:34 Michael Van Canneyt Relationship added related to 0031231
2020-11-24 09:11 Ondrej Pokorny Relationship added parent of 0038124