Fix resource leaks#55
Draft
ThomasWaldmann wants to merge 5 commits into
Draft
Conversation
…ssion Previously, setupEncoder created a strong retain cycle by calling `Unmanaged.passRetained(self)` and storing this reference as an instance property `vtEncoderRef`. Since the property held a strong reference to `self` as a member of `self`, the object could never be automatically deallocated if abandoned, leaking the session and its underlying VTCompressionSession. To resolve this: - Removed the `vtEncoderRef` property entirely. - Passed `self` as an unretained reference callback context (`Unmanaged.passUnretained(self)`) into `VTCompressionSessionCreate`. - Marked `vtEncoder` as `nonisolated(unsafe)` to satisfy Swift 6 strict concurrency checks inside the nonisolated deinitializer. - Implemented a `deinit` block to cleanly invalidate `vtEncoder` when the session is deallocated.
- Added deinit blocks to TBReceiverDiscovery and TBDirectDisplayStreamCapture to automatically stop Bonjour service searches and display streams on deallocation. - Updated TBDisplaySenderSession deinit to asynchronously invalidate repeating timers, cancel active NWConnection sockets, and stop screen capture streams on the main queue. - Annotated thread-confined session properties with nonisolated(unsafe) to conform to strict Swift 6 concurrency rules when accessing actor-isolated state from nonisolated deinit. - Verified clean compilation and successful builds of the Sender target.
…gement configuration - Added defer blocks to configureDesktopMirror and configureExtendedDesktop to ensure display configuration contexts are always completed or cancelled cleanly on failure or loop exits. - Removed redundant manual CGCancelDisplayConfiguration calls to simplify exception-safe control flow.
…eaming session callbacks - Replaced unsafe raw mutable pointers (`UnsafeMutableRawPointer`) in `TBDirectDisplayStreamCapture` and `setupEncoder` callbacks with a thread-safe, Sendable `WeakSessionBox` reference-counting helper. This prevents use-after-free and dangling-pointer crashes when background display capture or video encoding threads fire callbacks after the parent `TBDisplaySenderSession` is stopped or deallocated. - Introduced `SendableIOSurface` wrapper to securely bridge `IOSurface` across actor boundaries under Swift 6 strict concurrency checks without relying on manual retain/release reference counting (`Unmanaged.passRetained` / `takeRetainedValue`). This utilizes Swift ARC to fully prevent permanent IOSurface leaks if dispatch queues drop blocks or fail early guards. - Implemented synchronous encoder invalidation and context pointer release in `deinit` and `stop()` to eliminate race conditions between the deallocation sequence and background compression output callbacks.
… churn - Implemented an NSCursor reference/equality caching mechanism (`lastCheckedCursor` and `lastCheckedCursorType`) inside `TBDisplaySenderSession.getCurrentCursorType()`. - This avoids repeated high-frequency PNG serialization, NSGraphicsContext creation, and dictionary lookups of the system cursor image (running at 120Hz). - During stationary or unchanging cursor states, the expensive rendering path is completely bypassed, dropping the tracking loop CPU and memory allocation churn rate to virtually 0% while maintaining perfect responsiveness upon cursor transitions.
Contributor
Author
|
I just told Gemini "fix resource/memory leaks" multiple times, this is the result for review. |
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.
No description provided.