Skip to content

Commit ad91b2d

Browse files
alirezanetMizux
authored andcommitted
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
1 parent 447ab9b commit ad91b2d

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

ortools/sat/csharp/CpSolver.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Google.OrTools.Sat
2626
* variables in the best solution, as well as general statistics of the search.
2727
* </remarks>
2828
*/
29-
public sealed class CpSolver : IDisposable
29+
public class CpSolver : IDisposable
3030
{
3131
private LogCallback? _log_callback;
3232
private BestBoundCallback? _best_bound_callback;
@@ -207,19 +207,36 @@ public bool BooleanValue(ILiteral literal)
207207

208208
public string SolutionInfo() => Response!.SolutionInfo;
209209

210-
public void Dispose()
210+
/// <summary>
211+
/// Releases unmanaged resources and optionally releases managed resources.
212+
/// </summary>
213+
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
214+
protected virtual void Dispose(bool disposing)
211215
{
212216
if (_disposed)
213217
{
214218
return;
215219
}
216220

217-
_best_bound_callback?.Dispose();
218-
_log_callback?.Dispose();
219-
ReleaseSolveWrapper();
221+
if (disposing)
222+
{
223+
_best_bound_callback?.Dispose();
224+
_log_callback?.Dispose();
225+
ReleaseSolveWrapper();
226+
}
227+
220228
_disposed = true;
221229
}
222230

231+
/// <summary>
232+
/// Releases all resources used by the CpSolver.
233+
/// </summary>
234+
public void Dispose()
235+
{
236+
Dispose(true);
237+
GC.SuppressFinalize(this);
238+
}
239+
223240
[MethodImpl(MethodImplOptions.Synchronized)]
224241
private void CreateSolveWrapper()
225242
{
@@ -253,4 +270,4 @@ class BestBoundCallbackDelegate : BestBoundCallback
253270
public override void NewBestBound(double bound) => _delegate(bound);
254271
}
255272

256-
} // namespace Google.OrTools.Sat
273+
} // namespace Google.OrTools.Sat

0 commit comments

Comments
 (0)