Skip to content

macOS crash on NSEventTypeFlagsChanged due to characters selector #2

@Milky2018

Description

@Milky2018

Summary

On macOS, Milky2018/window crashes when the backend receives an NSEventTypeFlagsChanged event, because mbw_content_view_flags_changed calls -[NSEvent characters] on that event.

This is reproducible downstream in Milky2018/mgstudio by running the examples/2d/sprite_flipping example on macOS native. The app aborts immediately with NSInternalInconsistencyException.

Current behavior

Current backend code in macos/backend.c:

  • mbw_content_view_flags_changed(...)
  • calls mbw_event_nsstring_utf8(effective_event, "characters", ...)
  • also calls charactersIgnoringModifiers

But AppKit raises on characters for FlagsChanged events:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid message sent to event "NSEvent: type=FlagsChanged ..."'
...
AppKit  -[NSEvent characters]
libmacos.dylib mbw_content_view_flags_changed

Repro

  1. Run a native app using Milky2018/window on macOS.
  2. Trigger or receive a modifier-state change event (FlagsChanged).
  3. The process aborts in -[NSEvent characters].

In our downstream repo the repro command is:

moon run examples/2d/sprite_flipping

Repo: https://github.com/moonbit-community/mgstudio-engine

Expected behavior

FlagsChanged should be handled without calling selectors that are invalid for that event type. Modifier transitions should emit key/modifier updates, but the app must not crash.

Suggested direction

Guard the text extraction path for FlagsChanged events. At minimum, avoid characters on this event kind and fall back to scancode/modifier-derived data instead. If text is still needed, only use selectors AppKit permits for this event type.

Notes

Current upstream main still contains this code path in macos/backend.c around mbw_content_view_flags_changed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions