Skip to content

Commit

Permalink
Fixes CA2213 Disposable fields should be disposed (except for IndexWr…
Browse files Browse the repository at this point in the history
…iter and subclasses which need more work)
  • Loading branch information
NightOwl888 committed Nov 9, 2020
1 parent 0eb90e6 commit 7f52580
Show file tree
Hide file tree
Showing 69 changed files with 453 additions and 53 deletions.
24 changes: 24 additions & 0 deletions src/Lucene.Net.Analysis.OpenNLP/OpenNLPLemmatizerFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,29 @@ private void Clear()
lemmas = null;
lemmaNum = 0;
}

/// <summary>
/// Releases resources used by the <see cref="OpenNLPLemmatizerFilter"/> and
/// if overridden in a derived class, optionally releases unmanaged resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources;
/// <c>false</c> to release only unmanaged resources.</param>

// LUCENENET specific
protected override void Dispose(bool disposing)
{
try
{
if (disposing)
{
sentenceTokenAttrsIter?.Dispose();
sentenceTokenAttrsIter = null;
}
}
finally
{
base.Dispose(disposing);
}
}
}
}
25 changes: 25 additions & 0 deletions src/Lucene.Net.Analysis.SmartCn/HMMChineseTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,32 @@ protected override bool IncrementWord()
public override void Reset()
{
base.Reset();
tokens?.Dispose(); // LUCENENET specific: Dispose tokens before letting it go out of scope
tokens = null;
}

/// <summary>
/// Releases resources used by the <see cref="HMMChineseTokenizer"/> and
/// if overridden in a derived class, optionally releases unmanaged resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources;
/// <c>false</c> to release only unmanaged resources.</param>

// LUCENENET specific
protected override void Dispose(bool disposing)
{
try
{
if (disposing)
{
tokens?.Dispose(); // LUCENENET specific - dispose tokens and set to null
tokens = null;
}
}
finally
{
base.Dispose(disposing);
}
}
}
}
25 changes: 25 additions & 0 deletions src/Lucene.Net.Analysis.SmartCn/WordTokenFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,32 @@ public override bool IncrementToken()
public override void Reset()
{
base.Reset();
tokenIter?.Dispose(); // LUCENENET specific
tokenIter = null;
}

/// <summary>
/// Releases resources used by the <see cref="WordTokenFilter"/> and
/// if overridden in a derived class, optionally releases unmanaged resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources;
/// <c>false</c> to release only unmanaged resources.</param>

// LUCENENET specific
protected override void Dispose(bool disposing)
{
try
{
if (disposing)
{
tokenIter?.Dispose(); // LUCENENET specific - dispose tokenIter and set to null
tokenIter = null;
}
}
finally
{
base.Dispose(disposing);
}
}
}
}
7 changes: 7 additions & 0 deletions src/Lucene.Net.Benchmark/ByTask/Feeds/DirContentSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,17 @@ public virtual void Reset()
return null;
}

/// <summary>
/// Releases resources used by the <see cref="DirContentSource"/> and
/// if overridden in a derived class, optionally releases unmanaged resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources;
/// <c>false</c> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
inputFiles?.Dispose(); // LUCENENET specific - dispose inputFiles
inputFiles = null;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Lucene.Net.Benchmark/ByTask/Feeds/DocMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
m_source.Dispose();
m_source?.Dispose();
leftovr?.Dispose(); // LUCENENET specific
docState?.Dispose(); // LUCENENET specific
}
}

Expand Down
23 changes: 19 additions & 4 deletions src/Lucene.Net.Benchmark/ByTask/Feeds/TrecContentSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private void Read(StringBuilder buf, string lineStart,

internal virtual void OpenNextFile()
{
Dispose();
DoClose();
//currPathType = null;
while (true)
{
Expand Down Expand Up @@ -216,7 +216,7 @@ internal virtual void OpenNextFile()
return null;
}

protected override void Dispose(bool disposing)
private void DoClose() // LUCENENET specific - separate disposing from closing so those tasks that "reopen" can continue
{
if (reader == null)
{
Expand All @@ -225,7 +225,7 @@ protected override void Dispose(bool disposing)

try
{
reader.Dispose();
reader?.Dispose();
}
catch (IOException e)
{
Expand All @@ -238,6 +238,21 @@ protected override void Dispose(bool disposing)
reader = null;
}

/// <summary>
/// Releases resources used by the <see cref="TrecContentSource"/> and
/// if overridden in a derived class, optionally releases unmanaged resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources;
/// <c>false</c> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
DoClose();
trecDocBuffer?.Dispose(); // LUCENENET specific
}
}

public override DocData GetNextDocData(DocData docData)
{
string name = null;
Expand Down Expand Up @@ -293,7 +308,7 @@ public override void ResetInputs()
lock (@lock)
{
base.ResetInputs();
Dispose();
DoClose();
nextFile = 0;
iteration = 0;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Lucene.Net.Benchmark/ByTask/PerfRunData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,20 @@ public class PerfRunData : IDisposable

// objects used during performance test run
// directory, analyzer, docMaker - created at startup.
// reader, writer, searcher - maintained by basic tasks.
// reader, writer, searcher - maintained by basic tasks.
#pragma warning disable CA2213 // Disposable fields should be disposed
private Store.Directory directory;
#pragma warning restore CA2213 // Disposable fields should be disposed
private IDictionary<string, AnalyzerFactory> analyzerFactories = new Dictionary<string, AnalyzerFactory>();
private Analyzer analyzer;
private DocMaker docMaker;
private ContentSource contentSource;
private FacetSource facetSource;
private CultureInfo locale;

#pragma warning disable CA2213 // Disposable fields should be disposed
private Store.Directory taxonomyDir;
#pragma warning restore CA2213 // Disposable fields should be disposed
private ITaxonomyWriter taxonomyWriter;
private TaxonomyReader taxonomyReader;

Expand Down
27 changes: 26 additions & 1 deletion src/Lucene.Net.Benchmark/ByTask/Tasks/AddIndexesTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,33 @@ public override void SetParams(string @params)

public override void TearDown()
{
inputDir.Dispose();
inputDir?.Dispose();
inputDir = null; // LUCENENET specific
base.TearDown();
}

/// <summary>
/// Releases resources used by the <see cref="AddIndexesTask"/> and
/// if overridden in a derived class, optionally releases unmanaged resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources;
/// <c>false</c> to release only unmanaged resources.</param>

// LUCENENET specific
protected override void Dispose(bool disposing)
{
try
{
if (disposing)
{
inputDir?.Dispose(); // LUCENENET specific - dispose tokens and set to null
inputDir = null;
}
}
finally
{
base.Dispose(disposing);
}
}
}
}
23 changes: 23 additions & 0 deletions src/Lucene.Net.Benchmark/ByTask/Tasks/ConsumeContentSourceTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,28 @@ public override int DoLogic()
dd.Value = source.GetNextDocData(dd.Value);
return 1;
}

/// <summary>
/// Releases resources used by the <see cref="ConsumeContentSourceTask"/> and
/// if overridden in a derived class, optionally releases unmanaged resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources;
/// <c>false</c> to release only unmanaged resources.</param>

// LUCENENET specific
protected override void Dispose(bool disposing)
{
try
{
if (disposing)
{
dd.Dispose(); // LUCENENET specific - dispose dd
}
}
finally
{
base.Dispose(disposing);
}
}
}
}
24 changes: 24 additions & 0 deletions src/Lucene.Net.Benchmark/ByTask/Tasks/ReadTokensTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,29 @@ private int Read(char[] c, int off, int len, bool returnZeroWhenComplete)

protected override void Dispose(bool disposing) { }
}

/// <summary>
/// Releases resources used by the <see cref="ReadTokensTask"/> and
/// if overridden in a derived class, optionally releases unmanaged resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources;
/// <c>false</c> to release only unmanaged resources.</param>

// LUCENENET specific
protected override void Dispose(bool disposing)
{
try
{
if (disposing)
{
stringReader?.Dispose(); // LUCENENET specific - dispose stringReader and set to null
stringReader = null;
}
}
finally
{
base.Dispose(disposing);
}
}
}
}
2 changes: 2 additions & 0 deletions src/Lucene.Net.Codecs/BlockTerms/BlockTermsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public class BlockTermsWriter : FieldsConsumer
/// <summary>Extension of terms file</summary>
public readonly static string TERMS_EXTENSION = "tib";

#pragma warning disable CA2213 // Disposable fields should be disposed
protected IndexOutput m_output;
#pragma warning restore CA2213 // Disposable fields should be disposed
private readonly PostingsWriterBase postingsWriter;
//private readonly FieldInfos fieldInfos; // LUCENENET: Not used
private FieldInfo currentField;
Expand Down
2 changes: 2 additions & 0 deletions src/Lucene.Net.Codecs/BlockTerms/FixedGapTermsIndexWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ namespace Lucene.Net.Codecs.BlockTerms
/// </summary>
public class FixedGapTermsIndexWriter : TermsIndexWriterBase
{
#pragma warning disable CA2213 // Disposable fields should be disposed
protected IndexOutput m_output;
#pragma warning restore CA2213 // Disposable fields should be disposed

/// <summary>Extension of terms index file</summary>
internal readonly static string TERMS_INDEX_EXTENSION = "tii";
Expand Down
2 changes: 2 additions & 0 deletions src/Lucene.Net.Codecs/Memory/DirectDocValuesConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ namespace Lucene.Net.Codecs.Memory
/// </summary>
internal class DirectDocValuesConsumer : DocValuesConsumer
{
#pragma warning disable CA2213 // Disposable fields should be disposed
private IndexOutput data, meta;
#pragma warning restore CA2213 // Disposable fields should be disposed
//private readonly int maxDoc; // LUCENENET: Not used

internal DirectDocValuesConsumer(SegmentWriteState state, string dataCodec, string dataExtension,
Expand Down
2 changes: 2 additions & 0 deletions src/Lucene.Net.Codecs/Memory/FSTOrdTermsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ public class FSTOrdTermsWriter : FieldsConsumer
private readonly PostingsWriterBase postingsWriter;
//private readonly FieldInfos fieldInfos; // LUCENENET: Never read
private readonly IList<FieldMetaData> _fields = new List<FieldMetaData>();
#pragma warning disable CA2213 // Disposable fields should be disposed
private IndexOutput blockOut = null;
private IndexOutput indexOut = null;
#pragma warning restore CA2213 // Disposable fields should be disposed

public FSTOrdTermsWriter(SegmentWriteState state, PostingsWriterBase postingsWriter)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Lucene.Net.Codecs/Memory/FSTTermsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ public class FSTTermsWriter : FieldsConsumer

private readonly PostingsWriterBase _postingsWriter;
//private readonly FieldInfos _fieldInfos; // LUCENENET: Never read
#pragma warning disable CA2213 // Disposable fields should be disposed
private IndexOutput _output;
#pragma warning restore CA2213 // Disposable fields should be disposed
private readonly IList<FieldMetaData> _fields = new List<FieldMetaData>();

public FSTTermsWriter(SegmentWriteState state, PostingsWriterBase postingsWriter)
Expand Down
2 changes: 2 additions & 0 deletions src/Lucene.Net.Codecs/Memory/MemoryDocValuesConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ namespace Lucene.Net.Codecs.Memory
/// </summary>
internal class MemoryDocValuesConsumer : DocValuesConsumer
{
#pragma warning disable CA2213 // Disposable fields should be disposed
private IndexOutput data, meta;
#pragma warning restore CA2213 // Disposable fields should be disposed
private readonly int maxDoc;
private readonly float acceptableOverheadRatio;

Expand Down
Loading

0 comments on commit 7f52580

Please sign in to comment.