Skip to content

Commit 410b1af

Browse files
committed
C#: Properly dispose diagnostic writer objects
1 parent 55226c4 commit 410b1af

File tree

6 files changed

+23
-5
lines changed

6 files changed

+23
-5
lines changed

cpp/autobuilder/Semmle.Autobuild.Cpp.Tests/BuildScripts.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ internal class TestDiagnosticWriter : IDiagnosticsWriter
203203
public IList<DiagnosticMessage> Diagnostics { get; } = new List<DiagnosticMessage>();
204204

205205
public void AddEntry(DiagnosticMessage message) => this.Diagnostics.Add(message);
206+
207+
public void Dispose() { }
206208
}
207209

208210
/// <summary>

cpp/autobuilder/Semmle.Autobuild.Cpp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static int Main()
1717
try
1818
{
1919
Console.WriteLine("CodeQL C++ autobuilder");
20-
var builder = new CppAutobuilder(actions, options);
20+
using var builder = new CppAutobuilder(actions, options);
2121
return builder.AttemptBuild();
2222
}
2323
catch (InvalidEnvironmentException ex)

csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ internal class TestDiagnosticWriter : IDiagnosticsWriter
218218
public IList<DiagnosticMessage> Diagnostics { get; } = new List<DiagnosticMessage>();
219219

220220
public void AddEntry(DiagnosticMessage message) => this.Diagnostics.Add(message);
221+
222+
public void Dispose() { }
221223
}
222224

223225
/// <summary>

csharp/autobuilder/Semmle.Autobuild.CSharp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static int Main()
1717
try
1818
{
1919
Console.WriteLine("CodeQL C# autobuilder");
20-
var builder = new CSharpAutobuilder(actions, options);
20+
using var builder = new CSharpAutobuilder(actions, options);
2121
return builder.AttemptBuild();
2222
}
2323
catch (InvalidEnvironmentException ex)

csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public interface IAutobuilder<out TAutobuildOptions> where TAutobuildOptions : A
9292
/// The overall design is intended to be extensible so that in theory,
9393
/// it should be possible to add new build rules without touching this code.
9494
/// </summary>
95-
public abstract class Autobuilder<TAutobuildOptions> : IAutobuilder<TAutobuildOptions> where TAutobuildOptions : AutobuildOptionsShared
95+
public abstract class Autobuilder<TAutobuildOptions> : IDisposable, IAutobuilder<TAutobuildOptions> where TAutobuildOptions : AutobuildOptionsShared
9696
{
9797
/// <summary>
9898
/// Full file paths of files found in the project directory, as well as
@@ -370,6 +370,20 @@ protected BuildScript AutobuildFailure() =>
370370
}
371371
});
372372

373+
public void Dispose()
374+
{
375+
Dispose(true);
376+
GC.SuppressFinalize(this);
377+
}
378+
379+
protected virtual void Dispose(bool disposing)
380+
{
381+
if (disposing)
382+
{
383+
diagnostics?.Dispose();
384+
}
385+
}
386+
373387
/// <summary>
374388
/// Value of CODEQL_EXTRACTOR_<LANG>_ROOT environment variable.
375389
/// </summary>

csharp/extractor/Semmle.Util/ToolStatusPage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public DiagnosticMessage(
182182
/// <summary>
183183
/// Provides the ability to write diagnostic messages to some output.
184184
/// </summary>
185-
public interface IDiagnosticsWriter
185+
public interface IDiagnosticsWriter : IDisposable
186186
{
187187
/// <summary>
188188
/// Adds <paramref name="message" /> as a new diagnostics entry.
@@ -195,7 +195,7 @@ public interface IDiagnosticsWriter
195195
/// A wrapper around an underlying <see cref="StreamWriter" /> which allows
196196
/// <see cref="DiagnosticMessage" /> objects to be serialized to it.
197197
/// </summary>
198-
public sealed class DiagnosticsStream : IDiagnosticsWriter, IDisposable
198+
public sealed class DiagnosticsStream : IDiagnosticsWriter
199199
{
200200
private readonly JsonSerializer serializer;
201201
private readonly StreamWriter writer;

0 commit comments

Comments
 (0)