From dadd4b9e5764fd33dd68d6c7af76f5e67829f013 Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Mon, 25 Mar 2024 13:49:46 +0100 Subject: [PATCH] C#: Properly dispose diagnostic writer objects --- cpp/autobuilder/Semmle.Autobuild.Cpp/Program.cs | 2 +- .../Semmle.Autobuild.CSharp/Program.cs | 2 +- .../Semmle.Autobuild.Shared/Autobuilder.cs | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/cpp/autobuilder/Semmle.Autobuild.Cpp/Program.cs b/cpp/autobuilder/Semmle.Autobuild.Cpp/Program.cs index 5fca6f556fbc4..48a6528a65b8b 100644 --- a/cpp/autobuilder/Semmle.Autobuild.Cpp/Program.cs +++ b/cpp/autobuilder/Semmle.Autobuild.Cpp/Program.cs @@ -17,7 +17,7 @@ static int Main() try { Console.WriteLine("CodeQL C++ autobuilder"); - var builder = new CppAutobuilder(actions, options); + using var builder = new CppAutobuilder(actions, options); return builder.AttemptBuild(); } catch (InvalidEnvironmentException ex) diff --git a/csharp/autobuilder/Semmle.Autobuild.CSharp/Program.cs b/csharp/autobuilder/Semmle.Autobuild.CSharp/Program.cs index 71289148916b8..bec0376eac3a8 100644 --- a/csharp/autobuilder/Semmle.Autobuild.CSharp/Program.cs +++ b/csharp/autobuilder/Semmle.Autobuild.CSharp/Program.cs @@ -17,7 +17,7 @@ public static int Main() try { Console.WriteLine("CodeQL C# autobuilder"); - var builder = new CSharpAutobuilder(actions, options); + using var builder = new CSharpAutobuilder(actions, options); return builder.AttemptBuild(); } catch (InvalidEnvironmentException ex) diff --git a/csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs b/csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs index a23d29d2979cc..14768860a70be 100644 --- a/csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs +++ b/csharp/autobuilder/Semmle.Autobuild.Shared/Autobuilder.cs @@ -92,7 +92,7 @@ public interface IAutobuilder where TAutobuildOptions : A /// The overall design is intended to be extensible so that in theory, /// it should be possible to add new build rules without touching this code. /// - public abstract class Autobuilder : IAutobuilder where TAutobuildOptions : AutobuildOptionsShared + public abstract class Autobuilder : IDisposable, IAutobuilder where TAutobuildOptions : AutobuildOptionsShared { /// /// Full file paths of files found in the project directory, as well as @@ -370,6 +370,20 @@ protected BuildScript AutobuildFailure() => } }); + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + (diagnostics as IDisposable)?.Dispose(); + } + } + /// /// Value of CODEQL_EXTRACTOR__ROOT environment variable. ///