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

耗尽温柔 提交于 2019-12-24 05:49:52

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/53341781/do-i-need-to-call-dispose-on-the-standardanalyzer-or-does-dispose-on-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!