Skip to content

Commit da7888a

Browse files
authored
Merge pull request #4955 from alirezanet/fix/cpsolver-unsealed-dispose-pattern
Remove sealed modifier from CpSolver and implement standard dispose pattern
2 parents 7a4d996 + e52623e commit da7888a

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

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)