AnswerDebugging a resource leak

time to read 3 min | 461 words

As it turns out, there are a LOT of issues with this code:

public class QueueActions : IDisposable
    UnmanagedDatabaseConnection database;
    public string Name { get; private set; }

    public class QueueActions( UnmanagedDatabaseConnectionFactory factory)
         database = factory.Create();
         database.Open(()=> Name = database.ReadName());

   // assume proper GC finalizer impl

    public void Dispose()

And the code using this:

using(var factory = CreateFactory())
          using(var actions = new QueueActions(factory))

To begin with, what happens if we close the factory between the first and second lines in QueueActions constructors?

We already have an unmanaged resource, but when we try to open it, we are going to get an exception. Since the exception is thrown from the constructor, it will NOT invoke the usual using logic, and the code will not be disposed.

Furthermore, and the reason for the blog post about it. Dispose itself can also fail.

Here is the actual stack trace that caused this blog post:

Microsoft.Isam.Esent.Interop.EsentErrorException: Error TermInProgress (JET_errTermInProgress, Termination in progress)
at Microsoft.Isam.Esent.Interop.Api.Check(Int32 err) in Api.cs: line 1492
at Microsoft.Isam.Esent.Interop.Api.JetCloseTable(JET_SESID sesid, JET_TABLEID tableid) in Api.cs: line 372
at Microsoft.Isam.Esent.Interop.Table.ReleaseResource() in D:\Work\esent\EsentInterop\Table.cs: line 97
at Microsoft.Isam.Esent.Interop.EsentResource.Dispose() in EsentResource.cs: line 63
at Rhino.Queues.Storage.AbstractActions.Dispose() in AbstractActions.cs: line 146 

More posts in "Answer" series:

  1. (20 Jan 2017) What does this code do?
  2. (16 Aug 2011) Modifying execution approaches
  3. (30 Apr 2011) Stopping the leaks
  4. (24 Dec 2010) This code should never hit production
  5. (21 Dec 2010) Your own ThreadLocal
  6. (11 Feb 2010) Debugging a resource leak
  7. (03 Sep 2009) The lazy loaded inheritance many to one association OR/M conundrum
  8. (04 Sep 2008) Don't stop with the first DSL abstraction
  9. (12 Jun 2008) How many tests?