Fix window cleanup logic on macOS#164
Conversation
07edecb to
8669f29
Compare
|
@glowcoil Beautiful code and great detailed descriptions here! thank you. I just have a basic question so please forgive me: Were you able to change the
..? In other words, how can I tell that its safe to switch to |
Engid
left a comment
There was a problem hiding this comment.
Optimistically approving but hoping to hear back from @maxjvh with some QA testing confirmation
|
@Engid The |
|
Tested this PR using the |
|
Excellent! |
Instead of trying to detect when to clean up the window based on the
NSView's retain count, require window cleanup to be initiated explicitly viaWindow::close,WindowHandle::close, or[NSWindowDelegate windowShouldClose:](in non-parented mode; called when the user clicks the "X" button). This fixes the leaks and use-after-frees that can be caused by the inherent unreliability of the retain count logic.As discussed in #153, this change essentially means that the
NSViewcreated by Baseview will not be suitable as the top-level view for an Audio Unit, since the Baseview API now requires that child windows be cleaned up by an explicit call toWindowHandle::close, and the only reliable signal for cleaning up an Audio Unit view is a call to[NSView dealloc]. However, this does not mean that Baseview cannot be used in the context of an Audio Unit; it just means that plugin frameworks must implement a compatibility layer with a wrapperNSView(which is the approach taken by JUCE).In order to implement this change:
WindowStateis stored in anRcrather than aBox.WindowHandleholds anRc<WindowState>so thatWindowHandle::closecan directly invoke window cleanup logic.WindowState::from_viewnow returns a clone of theRc<WindowState>held by theNSViewto ensure that it lives until the end of an event handler.NSViewis set as the window delegate, which allows it to receive thewindowShouldClose:call when the user clicks the "X" button, upon which it will dispatch theWillCloseevent and initiate window cleanup logic.Window::open_parentedandopen_blockingno longerreleasetheNSViewimmediately after attaching it. Instead, theNSViewis released as part of the cleanup logic inWindowInner::close.Window::resizenow checks if the window is open to avoid using theNSViewafter releasing it.releasemethod, theretain_count_after_buildfield, theParentHandlestruct, and theclose_requestedflag have all been removed.