RustAudio / baseview

low-level window system interface for audio plugin UIs
Apache License 2.0
267 stars 57 forks source link

Allow freeing NSView based on parent window closing notifications. #137

Closed kunalarya closed 1 year ago

kunalarya commented 1 year ago

In order to use third party crates that, under the hood, may increase the ref count of the given NSView, we add an additional "window closing" check by querying the given NSView for its NSWindow parent, and subscribing to the NSWindowWillCloseNotification message.

Note that in DAWs such as Reaper, we share a parent window with other plugins, so this approach is not reliable (since you can switch between different plugins but the window persists). Those cases may require another solution.

(Mostly) fixes #124

glowcoil commented 1 year ago

I am not really a fan of heuristic solutions like this. As you say, in situations like in Reaper where the NSWindow outlives Baseview's NSView, this notification won't occur; and, in the reverse situation, where the host closes the containing NSWindow but retains a reference to Baseview's NSView and then reparents it to another window, this notification will result in premature destruction of the Baseview window.

There was a discussion on the Rust Audio Discord several months ago about changing Baseview's API contract so that windows may only be closed via an explicit call to WindowHandle::close() or Window::close(), which would resolve the refcount cycle issue in a more principled way. I'll write that up in more detail on #124.