Fixed window crashes on macOS #219
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
(edit: I read the RustAudio policy disallow the PR with AI, so I'm gonna edit this issue after I fully understand what I changed...)
This was discussed on Dicord before, that the
open_parentedexample crashes only on macOS build.I also found that some examples in nih-plug crashes similarly because of null pointer dereference.
I finally found these issues are a bit different things but could fixed with the help of AI... I am not professional of low-level window system and obj-C api of macOS, so it could be too much modification.
I will attach the summary of the modification from the conversation with ChatGPT.
What was fixed
macOS parented windows: ensured WindowInner stores the parent ns_window, so
raw_window_handle()returns a valid window for child views (avoids null window handles that crashed softbuffer) — seesrc/macos/window.rs.Metal surface creation: made the custom NSView layer-backed with a CAMetalLayer and keep it in sync on scale/resize, so wgpu’s Metal backend no longer panics on a nil layer — see
src/macos/view.rs.Why it mattered
Child windows were handing a null ns_window to downstream libraries, leading to null-pointer panics when creating rendering surfaces.
The view wasn’t layer-backed, so
get_metal_layersaw nil and wgpu’s Metal backend aborted.How it was fixed
Extract the parent window from the parent’s RawWindowHandle and store it in the child WindowInner for parented windows.
Enable wantsLayer, attach a CAMetalLayer, set its contentsScale/frame at creation, and update them on backing-scale or size changes.
Suggested verification
Run the open_parented example on debug build; confirm no softbuffer null-pointer panic.
Run a nih-plug/examples/gain_gui_iced example on debug build.