Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App crashes when clicking on a menu item on macOS #202

Closed
Korne127 opened this issue Jun 24, 2024 · 6 comments · Fixed by #221
Closed

App crashes when clicking on a menu item on macOS #202

Korne127 opened this issue Jun 24, 2024 · 6 comments · Fixed by #221

Comments

@Korne127
Copy link

I added a dummy menu:

fn create_menu() {
    let menu = Menu::with_items(&[
        &Submenu::with_items(
            "",
            true,
            &[&MenuItem::new(
                "Menu item",
                true,
                Some(Accelerator::new(Some(Modifiers::SUPER), Code::KeyD)),
            )],
        )
        .unwrap(),
    ])
    .unwrap();
    menu.init_for_nsapp();
}

to a macOS application which uses winit.
However, when I click on the menu item (or any menu item if I add several), the app crashes with

unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed isize::MAX

@Korne127 Korne127 changed the title App crashes when clicking on a menu item on macOS. App crashes when clicking on a menu item on macOS Jun 24, 2024
@amrbashir
Copy link
Member

Does the error show an backtrace?

@Korne127
Copy link
Author

After running RUST_BACKTRACE=full cargo run, I get the following backtrace:

thread 'main' panicked at library/core/src/panicking.rs:156:5:
unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed isize::MAX
stack backtrace:
0: 0x10464fffc - std::backtrace_rs::backtrace::libunwind::trace::h6de1cbf3f672a4f8
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
1: 0x10464fffc - std::backtrace_rs::backtrace::trace_unsynchronized::hd0de2d5ef13b6f4d
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
2: 0x10464fffc - std::sys_common::backtrace::_print_fmt::h2a33510d9b3bb866
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:68:5
3: 0x10464fffc - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h01b2beffade888b2
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:44:22
4: 0x104669bf0 - core::fmt::rt::Argument::fmt::h5ddc0f22b2928899
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/fmt/rt.rs:142:9
5: 0x104669bf0 - core::fmt::write::hbadb443a71b75f23
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/fmt/mod.rs:1153:17
6: 0x10464e2c4 - std::io::Write::write_fmt::hc09d7755e3ead5f0
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/io/mod.rs:1843:15
7: 0x10464fe54 - std::sys_common::backtrace::_print::h3cd1786cbb1caf0f
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:47:5
8: 0x10464fe54 - std::sys_common::backtrace::print::h28349e5c25acbac7
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:34:9
9: 0x104651214 - std::panicking::default_hook::{{closure}}::hd24b6196784d991e
10: 0x104650ef8 - std::panicking::default_hook::hfcec80a2720c8c73
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:292:9
11: 0x104651b08 - std::panicking::rust_panic_with_hook::h84760468187ddc85
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:779:13
12: 0x1046514c8 - std::panicking::begin_panic_handler::{{closure}}::he666a5eb600a7203
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:649:13
13: 0x104650480 - std::sys_common::backtrace::__rust_end_short_backtrace::h592f44d2bf9f843f
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:171:18
14: 0x10465126c - rust_begin_unwind
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:645:5
15: 0x104674db0 - core::panicking::panic_nounwind_fmt::runtime::h951c730897f917a2
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:110:18
16: 0x104674db0 - core::panicking::panic_nounwind_fmt::h9f5e27491296451b
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:123:9
17: 0x104674e30 - core::panicking::panic_nounwind::h765f3648d339fa95
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:156:5
18: 0x104620aa8 - core::slice::raw::from_raw_parts::precondition_check::hda7d675d4997a9c5
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/intrinsics.rs:2799:21
19: 0x104620028 - core::slice::raw::from_raw_parts::h8e0f3497b6ca688b
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/slice/raw.rs:98:9
20: 0x104620028 - <alloc::vec::Vec<T,A> as core::ops::deref::Deref>::deref::h7344813a661265fd
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/alloc/src/vec/mod.rs:2764:18
21: 0x1045c86d4 - <alloc::string::String as core::ops::deref::Deref>::deref::h95f3ab6ec6e99f7a
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/alloc/src/string.rs:2478:43
22: 0x1045b9294 - muda::platform_impl::platform::fire_menu_item_click::h87d8be28bd72c50e
at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/muda-0.13.5/src/platform_impl/macos/mod.rs:965:44
23: 0x18bec1f44 -
24: 0x18bf8b6c8 -
25: 0x18c5320e4 -
26: 0x18bfc3a70 -
27: 0x18bfc3990 -
28: 0x18c528b78 -
29: 0x18c352ec4 -
30: 0x18c352c64 -
31: 0x18c3f91d4 -
32: 0x18c3f90c4 -
33: 0x18c3f8fa4 -
34: 0x18bf17a04 -
35: 0x18c5084f4 -
36: 0x18bd1037c -
37: 0x104590ac0 - <() as objc2::encode::EncodeArguments>::__invoke::hcbf3b8e50ba496ab
at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/encode.rs:437:26
38: 0x10458fc30 - objc2::runtime::message_receiver::msg_send_primitive::send::h74483b82819a23d9
at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/runtime/message_receiver.rs:173:18
39: 0x104589c50 - objc2::runtime::message_receiver::MessageReceiver::send_message::hcb9aeb6f4b6f1e7f
at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/runtime/message_receiver.rs:433:38
40: 0x10457b620 - objc2::__macro_helpers::msg_send::MsgSend::send_message::h0e084715e746e3d0
at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/__macro_helpers/msg_send.rs:27:31
41: 0x10457c3bc - objc2_app_kit::generated::__NSApplication::NSApplication::run::h8c45905089bf0f5a
at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/macros/extern_methods.rs:247:14
42: 0x1044fd5d0 - winit::platform_impl::platform::event_loop::EventLoop::run_on_demand::{{closure}}::{{closure}}::hf24c16c0ba4f2019
at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.30.3/src/platform_impl/macos/event_loop.rs:295:26
43: 0x1044fa2b8 - objc2::rc::autorelease::autoreleasepool::h8275d322f7571fd2
at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/objc2-0.5.2/src/rc/autorelease.rs:438:15
44: 0x1044fd51c - winit::platform_impl::platform::event_loop::EventLoop::run_on_demand::{{closure}}::hb6055b8c687b3dbe
at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.30.3/src/platform_impl/macos/event_loop.rs:281:13
45: 0x104503d68 - winit::platform_impl::platform::event_handler::EventHandler::set::h6673e0c69fc0c1e7
at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.30.3/src/platform_impl/macos/event_handler.rs:98:9
46: 0x104502930 - winit::platform_impl::platform::app_state::ApplicationDelegate::set_event_handler::hff3e2a65c79a6824
at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.30.3/src/platform_impl/macos/app_state.rs:172:9
47: 0x1044fd4b4 - winit::platform_impl::platform::event_loop::EventLoop::run_on_demand::he52fa3be8274db41
at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.30.3/src/platform_impl/macos/event_loop.rs:280:9
48: 0x1044fdb04 - winit::platform_impl::platform::event_loop::EventLoop::run::h3ad4f1691e6b81d4
at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.30.3/src/platform_impl/macos/event_loop.rs:267:9
49: 0x10450232c - winit::event_loop::EventLoop::run::h8c7fb8483417fa47
at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/winit-0.30.3/src/event_loop.rs:231:9
50: 0x1044fc0f0 - njutkermekj::main::h8fc269689e5997d0
at ~/Documents/Programmieren/Git Projekte als Contributor/njutkermekj/src/main.rs:23:2
51: 0x104506430 - core::ops::function::FnOnce::call_once::h06d55fabe8c7d102
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ops/function.rs:250:5
52: 0x104504dbc - std::sys_common::backtrace::__rust_begin_short_backtrace::h01d6a0c2ad4c7b9d
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:155:18
53: 0x1044fa1d4 - std::rt::lang_start::{{closure}}::h813bb0db7428a53f
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/rt.rs:166:18
54: 0x10464c2f0 - core::ops::function::impls::<impl core::ops::function::FnOnce for &F>::call_once::heaba8a29e0324069
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ops/function.rs:284:13
55: 0x10464c2f0 - std::panicking::try::do_call::h6e5fac4f4884d97b
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:552:40
56: 0x10464c2f0 - std::panicking::try::h2b98e2f3cf76cd78
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:516:19
57: 0x10464c2f0 - std::panic::catch_unwind::hba9c97319335c08b
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panic.rs:146:14
58: 0x10464c2f0 - std::rt::lang_start_internal::{{closure}}::h18efdcfb68f002e8
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/rt.rs:148:48
59: 0x10464c2f0 - std::panicking::try::do_call::ha793e16770aada4d
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:552:40
60: 0x10464c2f0 - std::panicking::try::h8aa812e3e1310d12
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:516:19
61: 0x10464c2f0 - std::panic::catch_unwind::h38c4879f2623185e
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panic.rs:146:14
62: 0x10464c2f0 - std::rt::lang_start_internal::h39923ab4c3913741
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/rt.rs:148:20
63: 0x1044fa1a0 - std::rt::lang_start::h8dc7672670128c6c
at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/rt.rs:165:17
64: 0x1044fc488 - _main
thread caused non-unwinding panic. aborting.
Abort trap: 6

@amrbashir
Copy link
Member

seems like a problem in objc code and maybe related to tauri-apps/tauri#10001 and the new rust releases starting from 1.78. Can you check if building in --release mode fixes this issue? or maybe using an older rust version

@Korne127
Copy link
Author

Korne127 commented Jun 29, 2024

Hey :)
Thanks for the quick response
When I use the --release mode, it also crashes, but with a different error:

Segmentation fault: 11

Interestingly, when I use the RUST_BACKTRACE=1 cargo run --release, I get a different error:

thread 'main' panicked at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/muda-0.13.5/src/platform_impl/macos/icon.rs:29:53:
called Result::unwrap() on an Err value: Format(FormatError { inner: ZeroWidth })


You said that this looks like a problem in objc code. I wanted to ask you: Do you think it would be possible to replace objc and possibly cocoa with objc2? It's a fork that is much better maintained (objc is unmaintained and had its last commit four years ago) and in which tons of objc bugs have been fixed and many other projects have replaced it or are in the process of replacing it.
Additionally, there is objc2-foundation, objc2-app-kit and objc2-quartz-core which replace the functionality in cocoa and have the further advantages of:

  • Actual type safety (everything isn't just an id pointer)
  • Proper memory management (no longer need to do retain/release calls, autorelease pools aren't as important)
  • Automatically generated bindings to almost everything in those frameworks

(See servo/core-foundation-rs#628 (comment))

Maybe @madsmtm could make a PR for this (as they have opened PRs doing this for some other projects as well (@madsmtm Btw thank you for your work on improving the macOS Rust ecosystem; it's really amazing)), otherwise I could try to do this although I don't have much experience with objc2 (yet).

@FabianLars
Copy link
Member

You said that this looks like a problem in objc code. I wanted to ask you: Do you think it would be possible to replace objc and possibly cocoa with objc2?

That's the plan for all of our repos :) It's just a non-trivial amount of work so it takes some time.

@madsmtm
Copy link
Contributor

madsmtm commented Jun 29, 2024

Maybe @madsmtm could make a PR for this

I'm going on vacation for a few weeks, so I won't have the time right now - besides, I think you should try! I'll gladly review a PR doing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants