Litedb: Finalizer shouldn't dispose managed code

Created on 27 Sep 2017  路  4Comments  路  Source: mbdavid/LiteDB

This code is potentially a problem

~LiteFileStream()
        {
            Dispose(false);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (!_disposed)
            {
                if (this.CanWrite)
                {
                    this.Flush();
                }
                _disposed = true;
            }
        }

It should be

if (!_disposed && disposing)

The reason being is that finalizer might be called when managed objects don't exist anymore. Finalizer is supposed to deal exclusively with unmanaged code only.

Most helpful comment

I got exception on this problem.

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
at LiteDB.StreamExtensions+<&rt;c__DisplayClass4_0.<TryLock&rt;b__0()
at LiteDB.FileHelper.TryExec(System.Action, System.TimeSpan)
at LiteDB.StreamExtensions.TryLock(System.IO.FileStream, Int64, Int64, System.TimeSpan)
at LiteDB.FileDiskService.Lock(LiteDB.LockState, System.TimeSpan)
at LiteDB.LockService.LockReserved()
at LiteDB.LockService.Reserved()
at LiteDB.LiteEngine.Transaction[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.String, Boolean, System.Func`2<LiteDB.CollectionPage,Int32&rt;)
at LiteDB.LiteEngine.Insert(System.String, System.Collections.Generic.IEnumerable`1<LiteDB.BsonDocument&rt;)
at LiteDB.LiteEngine.Insert(System.String, LiteDB.BsonDocument)
at LiteDB.LiteFileStream.WriteChunks()
at LiteDB.LiteFileStream.Flush()
at LiteDB.LiteFileStream.Dispose(Boolean)
at LiteDB.LiteFileStream.Finalize()

All 4 comments

Could that be related to why I'm getting this error with LiteDB 3.1.4?

System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=LiteDB
  StackTrace:
       at LiteDB.LiteFileStream.WriteChunks()
       at LiteDB.LiteFileStream.Flush()
       at LiteDB.LiteFileStream.Dispose(Boolean disposing)
       at LiteDB.LiteFileStream.Finalize()
  InnerException: 

@john1726 Only vaguely. What you are seeing is a bug in 3.1.4 which was later on fixed. You could either try beta 4.0 (as it is only post 3.1.4 version on nuget) or download sources.

I got exception on this problem.

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
at LiteDB.StreamExtensions+<&rt;c__DisplayClass4_0.<TryLock&rt;b__0()
at LiteDB.FileHelper.TryExec(System.Action, System.TimeSpan)
at LiteDB.StreamExtensions.TryLock(System.IO.FileStream, Int64, Int64, System.TimeSpan)
at LiteDB.FileDiskService.Lock(LiteDB.LockState, System.TimeSpan)
at LiteDB.LockService.LockReserved()
at LiteDB.LockService.Reserved()
at LiteDB.LiteEngine.Transaction[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.String, Boolean, System.Func`2<LiteDB.CollectionPage,Int32&rt;)
at LiteDB.LiteEngine.Insert(System.String, System.Collections.Generic.IEnumerable`1<LiteDB.BsonDocument&rt;)
at LiteDB.LiteEngine.Insert(System.String, LiteDB.BsonDocument)
at LiteDB.LiteFileStream.WriteChunks()
at LiteDB.LiteFileStream.Flush()
at LiteDB.LiteFileStream.Dispose(Boolean)
at LiteDB.LiteFileStream.Finalize()

Our customers also face the problem described above.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manny42 picture manny42  路  3Comments

sandolkakos picture sandolkakos  路  3Comments

muhamad picture muhamad  路  3Comments

axelgenus picture axelgenus  路  3Comments

dangershony picture dangershony  路  3Comments