From ad91b2ddd348e7df31ee4ff8e6aa0f9b3c7e39e3 Mon Sep 17 00:00:00 2001
From: AliReZa Sabouri <7004080+alirezanet@users.noreply.github.com>
Date: Wed, 17 Dec 2025 00:32:15 +0100
Subject: [PATCH] Remove sealed modifier and implement standard dispose pattern
for CpSolver
The sealed modifier was unnecessarily breaking inheritance for users who need
to extend CpSolver. The standard IDisposable pattern with protected virtual
Dispose(bool) allows safe inheritance while maintaining proper resource cleanup.
This change:
- Removes the sealed modifier from CpSolver class
- Implements the standard dispose pattern with protected virtual Dispose(bool)
- Allows derived classes to override disposal behavior safely
- Maintains backward compatibility for existing non-inheriting code
---
ortools/sat/csharp/CpSolver.cs | 29 +++++++++++++++++++++++------
1 file changed, 23 insertions(+), 6 deletions(-)
diff --git a/ortools/sat/csharp/CpSolver.cs b/ortools/sat/csharp/CpSolver.cs
index c2a1449ee0..9a5a89dc42 100644
--- a/ortools/sat/csharp/CpSolver.cs
+++ b/ortools/sat/csharp/CpSolver.cs
@@ -26,7 +26,7 @@ namespace Google.OrTools.Sat
* variables in the best solution, as well as general statistics of the search.
*
*/
-public sealed class CpSolver : IDisposable
+public class CpSolver : IDisposable
{
private LogCallback? _log_callback;
private BestBoundCallback? _best_bound_callback;
@@ -207,19 +207,36 @@ public sealed class CpSolver : IDisposable
public string SolutionInfo() => Response!.SolutionInfo;
- public void Dispose()
+ ///
+ /// Releases unmanaged resources and optionally releases managed resources.
+ ///
+ /// true to release both managed and unmanaged resources; false to release only unmanaged resources.
+ protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
- _best_bound_callback?.Dispose();
- _log_callback?.Dispose();
- ReleaseSolveWrapper();
+ if (disposing)
+ {
+ _best_bound_callback?.Dispose();
+ _log_callback?.Dispose();
+ ReleaseSolveWrapper();
+ }
+
_disposed = true;
}
+ ///
+ /// Releases all resources used by the CpSolver.
+ ///
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
[MethodImpl(MethodImplOptions.Synchronized)]
private void CreateSolveWrapper()
{
@@ -253,4 +270,4 @@ class BestBoundCallbackDelegate : BestBoundCallback
public override void NewBestBound(double bound) => _delegate(bound);
}
-} // namespace Google.OrTools.Sat
+} // namespace Google.OrTools.Sat
\ No newline at end of file