Skip to content

Inexplicable runtime error on M1-based macOS #1934

@ChristianIvicevic

Description

@ChristianIvicevic

I have been experimenting with winit and noticed that the following snippet has runtime errors on M1 chips:

use winit::dpi::LogicalSize;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::platform::run_return::EventLoopExtRunReturn;
use winit::window::WindowBuilder;

struct Application {
    event_loop: EventLoop<()>,
}

impl Application {
    pub fn initialize() -> Self {
        let event_loop = Self::init_window();

        Self { event_loop }
    }

    fn init_window() -> EventLoop<()> {
        let event_loop = EventLoop::new();
        let _window = WindowBuilder::new()
            .with_title("Hello, World")
            .with_inner_size(LogicalSize::new(800, 480))
            .build(&event_loop)
            .unwrap();
        event_loop
    }

    fn main_loop(&mut self) {
        let mut should_close = false;
        loop {
            self.event_loop.run_return(|event, _, control_flow| {
                *control_flow = ControlFlow::Exit;

                match event {
                    Event::WindowEvent {
                        event: WindowEvent::CloseRequested,
                        ..
                    } => should_close = true,
                    _ => (),
                }
            });
            if should_close {
                return;
            }
        }
    }
}

fn main() {
    let mut app = Application::initialize();
    app.main_loop();
}

When starting the compiled app a stacktrace is displayed and the application is still running however without a window since the window that initially pops up vanishes right away:

2021-05-08 11:02:32.695543+0200 sample[81272:1716168] [General] *** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array
2021-05-08 11:02:32.696191+0200 sample[81272:1716168] [General] (
	0   CoreFoundation                      0x00000001a124fdb8 __exceptionPreprocess + 240
	1   libobjc.A.dylib                     0x00000001a0f790a8 objc_exception_throw + 60
	2   CoreFoundation                      0x00000001a131abb4 -[__NSCFString characterAtIndex:].cold.1 + 0
	3   CoreFoundation                      0x00000001a116ea7c -[__NSArrayM objectAtIndex:] + 188
	4   sample                              0x00000001007177e0 _ZN64_$LT$$LP$A$C$$RP$$u20$as$u20$objc..message..MessageArguments$GT$6invoke17hfcfa89ce4923c108E + 104
	5   CoreFoundation                      0x00000001a11d0c60 __CFRunLoopDoObservers + 572
	6   CoreFoundation                      0x00000001a11d02c8 __CFRunLoopRun + 1052
	7   CoreFoundation                      0x00000001a11cf734 CFRunLoopRunSpecific + 600
	8   HIToolbox                           0x00000001a90cdb84 RunCurrentEventLoopInMode + 292
	9   HIToolbox                           0x00000001a90cd8f8 ReceiveNextEventCommon + 552
	10  HIToolbox                           0x00000001a90cd6b8 _BlockUntilNextEventMatchingListInModeWithFilter + 72
	11  AppKit                              0x00000001a39b94ec _DPSNextEvent + 836
	12  AppKit                              0x00000001a39b7e8c -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1292
	13  AppKit                              0x00000001a39a9d18 -[NSApplication run] + 596
	14  sample                              0x00000001007763a8 _ZN60_$LT$$LP$$RP$$u20$as$u20$objc..message..MessageArguments$GT$6invoke17hacaea74fed85c1cdE + 72
)

I can confirm that the very same code works on Intel-based macOS just fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    B - bugDang, that shouldn't have happenedDS - appkitAffects the AppKit/macOS backend

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions