Skip to content

Remove sealed modifier from CpSolver and implement standard dispose pattern#4955

Merged
lperron merged 2 commits intogoogle:mainfrom
alirezanet:fix/cpsolver-unsealed-dispose-pattern
Dec 17, 2025
Merged

Remove sealed modifier from CpSolver and implement standard dispose pattern#4955
lperron merged 2 commits intogoogle:mainfrom
alirezanet:fix/cpsolver-unsealed-dispose-pattern

Conversation

@alirezanet
Copy link
Contributor

Problem

The sealed modifier added to CpSolver in commit f2e2e49 breaks existing code that inherits from CpSolver. The sealed keyword was not necessary for implementing IDisposable and prevents legitimate inheritance scenarios.

Solution

This PR:

  • Removes the sealed modifier from the CpSolver class
  • Implements the standard .NET dispose pattern with protected virtual Dispose(bool disposing)
  • Adds XML documentation for the dispose methods
  • Maintains full backward compatibility for non-inheriting code

Benefits

  1. Allows inheritance: Users can now extend CpSolver for custom solver implementations
  2. Standard pattern: Follows the recommended .NET IDisposable pattern for classes that may be inherited
  3. Safe disposal: Derived classes can override Dispose(bool disposing) to add their own cleanup logic
  4. No breaking changes: Existing non-inheriting code continues to work exactly as before

Example Usage

With this change, users can safely inherit from CpSolver:

public class CustomCpSolver : CpSolver
{
    private SomeResource _customResource;

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            _customResource?.Dispose();
        }
        base.Dispose(disposing);
    }
}

Related to commit: f2e2e49

…olver

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
@google-cla
Copy link

google-cla bot commented Dec 16, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@Mizux Mizux added Bug Lang: .NET .Net wrapper issue Solver: CP-SAT Solver Relates to the CP-SAT solver labels Dec 17, 2025
@Mizux Mizux added this to the v10.0 Beta milestone Dec 17, 2025
@lperron lperron merged commit da7888a into google:main Dec 17, 2025
2 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Lang: .NET .Net wrapper issue Solver: CP-SAT Solver Relates to the CP-SAT solver

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants