Skip to content

Clarify MotorHat disposal/ownership semantics in README#2584

Open
raffaeler with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-dcmotor-disposal-documentation
Open

Clarify MotorHat disposal/ownership semantics in README#2584
raffaeler with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-dcmotor-disposal-documentation

Conversation

Copilot AI commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

The MotorHat README showed a DCMotor being created but never disposed, leaving it unclear whether motors need separate disposal, whether dispose order matters, and whether disposing both the MotorHat and its motors causes conflicts.

Confirmed from the source that there is no conflict: MotorHat.Dispose() stops all channels it created and disposes the underlying PCA9685, while DCMotor3Pwm.Dispose() only stops its PWM pins (never disposes them). Disposal is therefore idempotent and order-independent.

Changes

  • Added a "Resource management (disposing)" section to src/devices/MotorHat/README.md covering the ownership model:
    • Disposing the MotorHat alone releases every motor/servo/PWM channel it handed out.
    • Disposing motors as well is safe (stop-only); dispose order is irrelevant.
  • Included a corrected PumpController-style example forwarding disposal to the MotorHat.
public void Dispose()
{
    // Disposing the MotorHat stops and releases every motor/channel it created.
    // Order is not important and disposing the motors as well would be safe.
    _motorHat.Dispose();
}

@dotnet-policy-service dotnet-policy-service Bot added the area-device-bindings Device Bindings for audio, sensor, motor, and display hardware that can used with System.Device.Gpio label Jul 1, 2026
Copilot AI changed the title [WIP] Fix unclear documentation around disposing of DCMotor with MotorHat Clarify MotorHat disposal/ownership semantics in README Jul 1, 2026
Copilot AI requested a review from raffaeler July 1, 2026 14:24
@raffaeler raffaeler marked this pull request as ready for review July 1, 2026 15:05
Copilot AI review requested due to automatic review settings July 1, 2026 15:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the MotorHat device documentation to clarify resource ownership and disposal behavior when creating motors/servos/PWM channels from MotorHat, aiming to prevent confusion about whether returned objects must be disposed and in what order.

Changes:

  • Adds a new “Resource management (disposing)” section describing MotorHat ownership and cleanup responsibilities.
  • Provides updated usage guidance and a wrapper-class example that forwards disposal to MotorHat.

Comment on lines +50 to +52
- **Disposing the `MotorHat` is enough.** `MotorHat.Dispose()` stops every channel it handed out and then disposes the underlying PCA9685 (and its I2C device). You do not need to dispose the motors, servos or PWM channels separately.
- **Disposing a motor as well is safe.** Disposing a `DCMotor` created by `CreateDCMotor` only stops its PWM channels; it does not dispose them. Combined with the point above, disposing both the motor and the `MotorHat` will not throw or leave the board in a random state.
- **Order does not matter.** You can dispose the motors before or after the `MotorHat`; the result is the same.
Comment on lines +86 to +88
// Disposing the MotorHat stops and releases every motor/channel it created.
// Order is not important and disposing the motors as well would be safe.
_motorHat.Dispose();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-device-bindings Device Bindings for audio, sensor, motor, and display hardware that can used with System.Device.Gpio

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unclear documentation around disposing of DCMotor when used with MotorHat

3 participants