Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent resource leaks in mouse event handling #113

Merged
merged 1 commit into from
Jan 22, 2025

Conversation

2mawi2
Copy link

@2mawi2 2mawi2 commented Jan 18, 2025

This PR fixes a resource leak in the mouse event handling code that could cause input lag over time.
This should fix #111

Changes:

  • Added proper cleanup of Core Foundation objects in unregisterMouseCallback
  • Release event tap and run loop source when unregistering callbacks
  • Maintain proper order of operations: disable tap → remove source → release resources

Technical Details:
The fix addresses the ownership of Core Foundation objects created with CGEventTapCreate and CFMachPortCreateRunLoopSource. These functions return objects with +1 retain count that need to be properly released to prevent resource leaks.

Impact:
This should prevent the accumulation of unused event taps and run loop sources when listeners are restarted (e.g. after sleep/wake cycles), improving long-term performance and preventing potential input lag.

@artginzburg
Copy link
Owner

I spent some time profiling this. It appears that no new memory leaks were introduced, and no memory leaks were removed.

Anyway, it's a nice improvement to the code, makes it more concise. I hope we can keep it up. Going to merge this a bit later after discussing another approach in #111

@artginzburg artginzburg merged commit 804b1d4 into artginzburg:main Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Freezing/lagging dragging after certain time
2 participants