Uploaded image for project: 'Lucene.Net'
  1. Lucene.Net
  2. LUCENENET-511

ObjectDisposedException thrown when IndexWriter disposed by finalizer

VotersWatch issueWatchersLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Invalid
    • Lucene.Net 3.0.3
    • None
    • Lucene.Net Core
    • None
    • Windows 7 x64, .NET Framework 4.5

    Description

      I'm having an issue of ObjectDisposedException with an error message "Cannot access a closed file." when working IndexWriter.

      I manage to reproduce it when I create a new WPF (4.5) app and install 3.0.3-RC2 from NuGet. I then create a LuceneManager which implements IDisposable, and I create a finalizer for it. I wrap IndexWriter inside it, and dispose it inside Dispose(bool). The implementation code looks like this:

      namespace WpfApplication
      {
          using System;
          using System.Collections.Generic;
          using System.IO;
      
          using Lucene.Net.Analysis.Standard;
          using Lucene.Net.Index;
          using Lucene.Net.Store;
      
          using Version = Lucene.Net.Util.Version;
      
          public class LuceneManager : IDisposable
          {
              public static readonly string IndexPath =
                  Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Index");
      
              private IndexWriter _indexWriter;
      
              private bool _isDisposed;
      
              public LuceneManager()
              {
                  var directory = FSDirectory.Open(IndexPath);
                  var analyzer = new StandardAnalyzer(Version.LUCENE_30, new HashSet<string>());
                  this._indexWriter = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
              }
      
              ~LuceneManager()
              {
                  this.Dispose(false);
              }
      
              public void Dispose()
              {
                  this.Dispose(true);
                  GC.SuppressFinalize(this);
              }
      
              private void Dispose(bool isDisposing)
              {
                  if (!this._isDisposed)
                  {
                      if (this._indexWriter != null)
                      {
                          this._indexWriter.Dispose();
                      }
                      this._indexWriter = null;
      
                      this._isDisposed = true;
                  }
              }
          }
      }
      

      And the calling code looks like this:

      namespace WpfApplication
      {
          using System.Windows;
      
          public partial class MainWindow : Window
          {
              public MainWindow()
              {
                  this.InitializeComponent();
              }
      
              private void OnLoaded(object sender, RoutedEventArgs e)
              {
                  var lucenceManager = new LuceneManager();
              }
          }
      }
      

      The app run just fine until I close it and it throws ObjectDisposedException as described above.

      The exception details:

      System.ObjectDisposedException was unhandled
        HResult=-2146232798
        Message=Cannot access a closed file.
        Source=mscorlib
        ObjectName=""
        StackTrace:
             at System.IO.__Error.FileNotOpen()
             at System.IO.FileStream.get_Length()
             at Lucene.Net.Store.NativeFSLock.Release()
             at Lucene.Net.Index.IndexWriter.CloseInternal(Boolean waitForMerges)
             at Lucene.Net.Index.IndexWriter.Dispose(Boolean disposing, Boolean waitForMerges)
             at Lucene.Net.Index.IndexWriter.Dispose(Boolean waitForMerges)
             at Lucene.Net.Index.IndexWriter.Dispose()
             at WpfApplication.LuceneManager.Dispose(Boolean isDisposing)
             at WpfApplication.LuceneManager.Finalize()
        InnerException: 
      

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            mahara Maximilian Haru Raditya
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment