Search code examples
lucene.netdisposeidisposable

Do I need to call .Dispose() on the StandardAnalyzer, or does .Dispose() on the IndexWriter dispose its descendants?


Initializing an IndexWriter in Lucene.Net looks like this:

var analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(version);
var indexWriterConfig = new Lucene.Net.Index.IndexWriterConfig(version, analyzer);
Index = new Lucene.Net.Index.IndexWriter(luceneDir, indexWriterConfig);

That is, you can't instantiate an IndexWriter without an Analyzer. So, I would expect that calling .Dispose() on the IndexWriter would dispose its children, including the Analyzer. However browsing the code I don't see that happening - so far. Have I missed it?

So: Does calling .Dispose() on the IndexWriter dispose the Analyzer, and if not, why not?


Solution

  • IndexWriter does not dispose of the analyzer.

    It doesn't dispose of the analyzer because it cannot be sure that you will not be using the analyzer elsewhere. It's a reference that it got via the constructor, it could be used by other IndexWriter instances without it knowing.

    It's about ownership; you created the analyzer and let the writer use it. It is thus your responsibility to dispose of the analyzer.