Open
Clarify MotorHat disposal/ownership semantics in README#2584
Conversation
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
raffaeler
approved these changes
Jul 1, 2026
Contributor
There was a problem hiding this comment.
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
MotorHatownership 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The MotorHat README showed a
DCMotorbeing created but never disposed, leaving it unclear whether motors need separate disposal, whether dispose order matters, and whether disposing both theMotorHatand 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, whileDCMotor3Pwm.Dispose()only stops its PWM pins (never disposes them). Disposal is therefore idempotent and order-independent.Changes
src/devices/MotorHat/README.mdcovering the ownership model:MotorHatalone releases every motor/servo/PWM channel it handed out.PumpController-style example forwarding disposal to theMotorHat.