Issue Details (XML | Word | Printable)

Key: DNET-316
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Jiri Cincura
Reporter: Gerdus van Zyl
Votes: 6
Watchers: 8

If you were logged in you would be able to see more operations.
.NET Data provider

Null reference exception on fbcommand dispose

Created: 14/May/10 09:13 AM   Updated: 04/Jan/14 08:16 AM
Component/s: ADO.NET Provider
Affects Version/s: 2.5.2, 2.6
Fix Version/s: 2.7

File Attachments: 1. Zip Archive (7.11 MB)

Windows 7, .net 3.5 sp1, visual studio 2008
Windows 7, .net 4, visual studio 2010
Issue Links:

 Description  « Hide
I have been getting the following exception if I close an connection (or forget to close connection manually) before explicitly calling .Dispose() on all FbCommands created (I now put fbcommand object in a list and dispose of manually before connection close). Which is strange since I can't see where I keep die commands alive beyond the life of the connection. The part of my program which gives the problem is a database schema upgrade that runs in a background thread.

Object reference not set to an instance of an object.

stack trace:
   at FirebirdSql.Data.FirebirdClient.FbConnectionInternal.RemovePreparedCommand(FbCommand command)
   at FirebirdSql.Data.FirebirdClient.FbCommand.Release()
   at FirebirdSql.Data.FirebirdClient.FbCommand.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Finalize()

target site:
{Void RemovePreparedCommand(FirebirdSql.Data.FirebirdClient.FbCommand)}

 All   Comments   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Gerdus van Zyl added a comment - 14/May/10 09:20 AM
Forgot to mention the exception isn't catchable, as in visual studio does not jump to the line in my program that gave the exception.

Marc Bettex added a comment - 17/Aug/10 02:44 PM - edited
I observed the same behavior. I don't know really what caused it in my case. It seems to happen kind of randomly, but it only when I have several threads that use the connector to query the database at the same time.

It seems that the FbConnectionInternal.preparedCommands field is cleared during the execution of the FbConnectionInternalRemovePerparedCommand, which causes an exception because the variable i is greater than zero but the size of the collection is zero.

I would have thought that this field was modified by another thread, but I'm not sure. I don't understand how another thread could modify this field, since it is the garbage collector that calls this method when the exception occurs, and it shouldn't be possible to use an object while it is being garbage collected. But I don't really see how the code in the loop could clear this field either. So yeah, I don't really have a clue of why this happens. It looks like very strange to me.

If you want to catch the exception in visual studio, type CTR + ALT + E in visual studio and check everything in the dialog. Then if the exception is thrown, the debugger will stop at this point.


Actually, after spending some more time, I found a place where I forgot to call dispose() on an FbCommand in my code. After adding that call and running tests that used to fail sometimes before, they don't fail anymore. It used to happen kind of randomly, so I cannot be sure that the problem is really solved, but it seems to work now. So yeah, Gerdus van Zyl is probably right when saying that the problem is caused by not calling dispose() an an FbCommand before the connection is closed.

Eugeniy added a comment - 27/Oct/10 10:56 AM
Same problem. But during work with Entity Framework.
It was attempt to transfer project from MS SQL to Firebird. In MS SQL all was fine. So it is not about manual calling of Dispose() on any Commands.
In the same time only one thread access Entity Context so it is not about multi-threading.

Problems with reproduction.

Such kind of problems prevent from using of Firebird in any real .NET projects based on Entity Framework. Handling errors in some specific command/query may have work-around. But such kind of errors like this one don't have realistic work-around.

I think that priority should be changed to Blocker or Critical.

Martin Turek added a comment - 29/Nov/10 07:36 AM - edited
I get sometimes a strange error while I use a program which was created with provider 2.5.1. Now I changed the version to 2.6 and get following error spontaneously, I can't say when and why it happens. Maybe you will know. It seems not to be cause by my application, in addition to that this error does not occur when I use the old provider version.

Error (sorry, but in german):

[4208] LPCS->Unhandled EXCEPTION -> OnCurrentDomain_UnhandledException
[4208] LPCS->EXCEPTION: System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
[4208] bei FirebirdSql.Data.FirebirdClient.FbConnectionInternal.RemovePreparedCommand(FbCommand command)
[4208] bei FirebirdSql.Data.FirebirdClient.FbCommand.Release()
[4208] bei FirebirdSql.Data.FirebirdClient.FbCommand.Dispose(Boolean disposing)
[4208] bei System.ComponentModel.Component.Finalize()
[4208] LPCS->System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
[4208] bei FirebirdSql.Data.FirebirdClient.FbConnectionInternal.RemovePreparedCommand(FbCommand command)
[4208] bei FirebirdSql.Data.FirebirdClient.FbCommand.Release()
[4208] bei FirebirdSql.Data.FirebirdClient.FbCommand.Dispose(Boolean disposing)
[4208] bei System.ComponentModel.Component.Finalize()

Gerdus van Zyl added a comment - 20/Jul/11 01:54 PM
recently upgraded to 2.6.5 in conjunction with .net version 4 and problem still exists. The problem is intermittent as all good heisenbugs should be.

Gerdus van Zyl added a comment - 20/Jul/11 01:55 PM
updated to inform that bug still exist in newest version and in .net4/vs2010

Mert Ali Özalp added a comment - 25/Aug/11 09:12 AM

I am using 4.0 and firebird 2.5 in my web site project. And i use firebird .net provider (for sure)

My web site is being used by only 5 - 10 users by now. I tried .net provider 2.5.2 and 2.6.5 but for both of them, I am getting the following exception for most of the time ....

Exception: System.NullReferenceException

Message: Object reference not set to an instance of an object.

StackTrace: at FirebirdSql.Data.FirebirdClient.FbCommand.RollbackImplicitTransaction()
   at FirebirdSql.Data.FirebirdClient.FbCommand.Release()
   at FirebirdSql.Data.FirebirdClient.FbCommand.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Finalize()

Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
   at FirebirdSql.Data.FirebirdClient.FbCommand.RollbackImplicitTransaction()
   at FirebirdSql.Data.FirebirdClient.FbCommand.Release()
   at FirebirdSql.Data.FirebirdClient.FbCommand.Dispose(Boolean)
   at System.ComponentModel.Component.Finalize()

But when only one user is using the site , no exception occurs, and when a second user enters the site, just at that time the exception occurs

and goes on occurring as long as more than one user is using the site.

I think there is problem in the user session management mechanism of the firebird .net provider ...

Could any one give me any advice or solution for this problem ?

Can A. Course added a comment - 21/Oct/11 09:04 PM
Happening to me as well but I am calling dispose() or using usings in the right places. Very random and hard to track down.

Leonard Wennekers added a comment - 01/Oct/12 05:57 AM
We are using FB 2.5.1 with the provider 2.7. 7 in a Windows 7, .net 4, visual studio 2010
environment and we still got those exceptions. It's hard to track down why the exception
occurs. It's once in a while but very unpleasant... I hope that other can point me out what
they did to solve this problem

Andrey added a comment - 08/Feb/13 04:12 AM
It still appears in NETProvider- with FB 2.5!! It is so frustrating

Jiri Cincura added a comment - 08/Feb/13 10:15 AM
Provide a test case.

Yiannis Bourkelis added a comment - 08/Oct/13 06:54 PM
I am also experiencing the same issue. The error message I get is:

Object reference not set to an instance of an object.

Stack Trace:
   at FirebirdSql.Data.FirebirdClient.FbCommand.Release()
   at FirebirdSql.Data.FirebirdClient.FbCommand.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Finalize()

This error pops up randomly, once every one or two days and causes my app to crash.

I am using firebird net provider 2.7.7, targeting net framework 2.0.
I see this error on a Windows Server 2008 R2 machine.
The database is accessed from multiple threads.

Any ideas how to fix this?

Bill Volz added a comment - 08/Oct/13 07:58 PM
Making sure that all your command & connection objects are wrapped in using tags will prevent this from occurring.


using(var conn = new FbConnection())
  using(var cmd = new FbCommand(conn,...))
If you want to reproduce this error create an IIS web application that uses the provider but don't call dispose on the objects. It may take a few attempts for it to occur but it will eventually occur.

Yiannis Bourkelis added a comment - 09/Oct/13 04:24 PM
Thank you Bill, I will try your suggestion.

If you have a working solution to reproduce this issue please post it here so that Jiri will be able to fix it.

I will also try to create a winforms reproducable test case and post it here

Yiannis Bourkelis added a comment - 04/Jan/14 08:16 AM
Finally I was able to create a reproducable case for this exception.

Steps to reproduce:

Inside the zip file you will find the folder FirebirdNullExceptionReproduce.
Open FirebirdNullExceptionReproduce.sln inside.

Run the app and wait for some seconds.
You sould be able to see one of the following exceptions:

Object reference not set to an instance of an object


Attempted to read or write protected memory. This is often an indication that other memory is corrupt.