hishnash / KeyWindow

Share values provided by views in the Key window to all other parts of your SwiftUI application including the commands block.
MIT License
45 stars 5 forks source link

Fix a memory leak after closing observed window. #5

Closed Oschly closed 3 years ago

Oschly commented 3 years ago

makeUIView(context:) in HostingWindowFinder has a weak reference to a new UIView instance, which causes memory leak (shown on image below) when window with view that has .observeWindow() modifier is deinitialized (closed). Instruments image with presente memory leak

Declaring window as separated property, then passing it to the closure looks like a valid fix for that issue (closing window is marked with a gray vertical mark on image below).

Instruments image without any memory leak

Oschly commented 3 years ago

Window's reference is nil at a time of pass passing it to the closure 🤦🏻‍♂️
For a time of fixing it, I'll close this PR

Oschly commented 3 years ago

Issue fixed by calling removeObserver on each of observers in WindowObserver.

hishnash commented 3 years ago

Sweet thankyou for spotting this.