Remove ComPortController finalizer and run deactivation on program shutdown#1427
Remove ComPortController finalizer and run deactivation on program shutdown#1427Copilot wants to merge 7 commits into
Conversation
|
@copilot can we register to the Program shutting down event (may need to reference other classes for this) and call deactivate at shutdown |
Implemented in d4f1fec. |
There was a problem hiding this comment.
Pull request overview
This PR updates ComPortController shutdown behavior to avoid COM-port cleanup running in the GC finalizer path and instead perform cleanup explicitly during program shutdown (eProgramStatusEventType.Stopping). The goal is to prevent shutdown-time exceptions and make deactivation safer and repeatable.
Changes:
- Removed the
~ComPortController()finalizer-based cleanup. - Added a
CrestronEnvironment.ProgramStatusEventHandlerhook to callDeactivate()on programStopping. - Made
Deactivate()guarded with a lock/flag and treatPort == nullas already deactivated, while also unsubscribingSerialDataReceived.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| RegisterAndConfigureComPort(); | ||
| }); | ||
|
|
||
| CrestronEnvironment.ProgramStatusEventHandler += CrestronEnvironment_ProgramStatusEventHandler; | ||
| } |
| lock (_deactivateLock) | ||
| { | ||
| if (_isDeactivated) | ||
| return true; | ||
|
|
ComPortControllercould throwNullReferenceExceptionduring shutdown from its finalizer path. The controller was performing COM-port cleanup in~ComPortController()even when no port instance existed.Finalization cleanup moved out of GC path
~ComPortController()to eliminate shutdown-time finalizer exceptions and avoid cleanup work during object finalization.Explicit shutdown behavior in
Deactivate()SerialDataReceivedinDeactivate().Port == nullas already deactivated (return true) instead of a failure condition.Program shutdown integration
ComPortControllertoCrestronEnvironment.ProgramStatusEventHandlerafter successful initialization.Deactivate()when the program entersStoppingso COM port cleanup runs during system shutdown without relying on finalization.Deactivate()now unsubscribes from the program status event and is guarded to be idempotent/thread-safe to prevent duplicate deactivation work.Net effect