Skip to content

Conversation

@huynguyenh
Copy link
Member

@huynguyenh huynguyenh commented Nov 6, 2025

Root Causes Identified

  1. DispatchQueue closures capturing self strongly, creating retain cycles
  2. NotificationCenter observers never being removed
  3. NSStatusItem accumulation without proper cleanup
  4. NSLayoutConstraint retention when views were removed

Fixes Implemented

1. DispatchQueue Closure Memory Leaks

Files: StatusBarController.swift:69-71, 142-144

  • Added [weak self] to async closures to prevent retain cycles
  • Fixes leak triggered on every user interaction

2. NotificationCenter Observer Cleanup

Files: StatusBarController.swift, PreferencesViewController.swift

  • Added deinit methods to properly remove observers
  • Cleans up .prefsChanged and .alwayHideToggle observers

3. NSStatusItem Proper Lifecycle

File: StatusBarController.swift:244-262

  • Only creates NSStatusItem if one doesn't exist
  • Properly calls NSStatusBar.system.removeStatusItem() before setting to nil

4. NSLayoutConstraint Memory Leak (Critical)

File: StackView+Extension.swift:14-15

  • Deactivates constraints before removing views
  • Prevents constraint retention of removed NSImageViews
  • This was the biggest leak source (9-10 views recreated repeatedly)

5. Timer Cleanup

File: StatusBarController.swift deinit

  • Explicitly invalidates timer in deinit

6. Deployment Target Update

File: project.pbxproj

  • Updated from macOS 10.12 to 10.13 (required by HotKey dependency)

- Add [weak self] to DispatchQueue closures to prevent retain cycles
- Implement deinit in StatusBarController and PreferencesViewController to clean up NotificationCenter observers
- Fix NSStatusItem to only create once and properly remove with removeStatusItem()
- Deactivate NSLayoutConstraints before removing views to prevent constraint memory leaks
- Update macOS deployment target from 10.12 to 10.13 (required by HotKey dependency)

These fixes resolve the cumulative memory leak that caused the app to grow to 2.89GB
over time on macOS Sequoia. Testing shows 79% reduction in memory growth on repeated operations.

Fixes #326
@phucledien
Copy link
Member

cool, thanks @huynguyenh , lemme check this on weekend 🙏

@rogersmj
Copy link

I'm glad someone found the cause of this...any possibility of expediting a release with this fix? It's really killing my Mac. Thank you.

@johnciprian
Copy link

When will this fix get merged?

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.

5 participants