superlistapp / super_native_extensions

Native drag & drop, clipboard access and context menu.
MIT License
364 stars 56 forks source link

[macos][super_drag_and_drop] example crash #359

Closed BruceChen5683 closed 2 weeks ago

BruceChen5683 commented 2 weeks ago

On a Mac with an Apple chip, when I run the example in the demo and use the touchpad to perform a two-finger zoom, it crashes. However, it runs normally on another Mac with an Intel chip.


Translated Report (Full Report Below)

Process: example [73403] Path: /Users/USER/*/example.app/Contents/MacOS/example Identifier: com.example.example Version: 1.0.0 (1) Code Type: ARM-64 (Native) Parent Process: dart [73074] Responsible: studio [35489] User ID: 501

Date/Time: 2024-05-13 18:03:35.0811 +0800 OS Version: macOS 14.2.1 (23C71) Report Version: 12 Anonymous UUID: 7F3C261B-BD56-2897-375D-477AAE9E629A

Sleep/Wake UUID: BE8D43C7-FF30-4C23-9CB1-CA5869E7CB2E

Time Awake Since Boot: 200000 seconds Time Since Wake: 16173 seconds

System Integrity Protection: enabled

Crashed Thread: 0 Dispatch queue: com.apple.main-thread

Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: KERN_PROTECTION_FAILURE at 0x000000016d35bfa0 Exception Codes: 0x0000000000000002, 0x000000016d35bfa0

Termination Reason: Namespace SIGNAL, Code 11 Segmentation fault: 11 Terminating Process: exc handler [73403]

VM Region Info: 0x16d35bfa0 is in 0x169b58000-0x16d35c000; bytes after start: 58736544 bytes before end: 95 REGION TYPE START - END [ VSIZE] PRT/MAX SHRMOD REGION DETAIL MALLOC_MEDIUM 160000000-168000000 [128.0M] rw-/rwx SM=PRV
GAP OF 0x1b58000 BYTES ---> STACK GUARD 169b58000-16d35c000 [ 56.0M] ---/rwx SM=NUL ... for thread 0 Stack 16d35c000-16db58000 [ 8176K] rw-/rwx SM=SHM thread 0

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 0 super_nativeextensions 0x103a3afcc $LT$usize$u20$as$u20$core..slice..index..SliceIndex$LT$$u5b$T$u5d$$GT$$GT$::get::h4600b6c1b6771aa6 + 12 1 super_nativeextensions 0x103a39570 core::str::$LT$impl$u20$str$GT$::is_char_boundary::hf34d418ad49a9cc2 + 84 2 super_nativeextensions 0x103a3efbc core::str::traits::$LT$impl$u20$core..slice..index..SliceIndex$LT$str$GT$$u20$for$u20$core..ops..range..Range$LT$usize$GT$$GT$::get::h04204157c139c5b5 + 104 3 super_nativeextensions 0x103a3f050 core::str::traits::$LT$impl$u20$core..slice..index..SliceIndex$LT$str$GT$$u20$for$u20$core..ops..range..Range$LT$usize$GT$$GT$::index::hf22ab9ab7b77bb0d + 64 4 super_nativeextensions 0x103a3f2e0 core::str::traits::$LT$impl$u20$core..ops..index..Index$LT$I$GT$$u20$for$u20$str$GT$::index::hf7d48347940e71a8 + 60 5 super_native_extensions 0x103a3d4a4 objc2_encode::parse::Parser::parse_container::h081505ca764e3bd3 + 308 6 super_native_extensions 0x103a3dbb4 objc2_encode::parse::Parser::parse_encoding_inner::h152501fdf547d5aa + 716 7 super_native_extensions 0x103a3d83c objc2_encode::parse::Parser::try_parse_encoding::h25682ae998f785ee + 108 8 super_native_extensions 0x103a3d790 objc2_encode::parse::Parser::parse_encoding::h436f272b4facd047 + 36 9 super_native_extensions 0x103a3db18 objc2_encode::parse::Parser::parse_encoding_inner::h152501fdf547d5aa + 560 10 super_native_extensions 0x103a3d83c objc2_encode::parse::Parser::try_parse_encoding::h25682ae998f785ee + 108 11 super_native_extensions 0x103a3d790 objc2_encode::parse::Parser::parse_encoding::h436f272b4facd047 + 36 12 super_native_extensions 0x103a3b5fc objc2_encode::encoding_box::EncodingBox::from_start_of_str::h460f4f400df230bf + 76 13 super_native_extensions 0x103a2a654 objc2::runtime::method_encoding_iter::MethodEncodingIter::extract_encoding::he5a51a81f64ec1f2 + 44 14 super_nativeextensions 0x103a2a830 $LT$objc2..runtime..method_encoding_iter..MethodEncodingIter$u20$as$u20$core..iter..traits..iterator..Iterator$GT$::next::h72236568c77b3bb8 + 68 15 super_native_extensions 0x103a2a15c objc2::runtime::method_encoding_iter::MethodEncodingIter::extract_return::h704960b48f8be823 + 40 16 super_native_extensions 0x103a22f18 objc2::verify::verify_method_signature::h6a2a4e7c6ffbc433 + 128 17 super_native_extensions 0x103a313b4 objc2::runtime::message_receiver::msg_send_check_class::h01e7e520f694e6dd + 128 18 super_native_extensions 0x103a3131c objc2::runtime::message_receiver::msg_send_check::h312e6ea71c8f6178 + 120 19 super_native_extensions 0x103658854 objc2::runtime::message_receiver::MessageReceiver::send_message::hb56731e8e43d3eae + 152 20 super_native_extensions 0x10372b26c objc2::macro_helpers::msg_send::MsgSend::send_message::h9ed17b407a190dff + 172 21 super_nativeextensions 0x1036faabc $LT$icrate..generated..AppKit..NSEvent..NSEvent$u20$as$u20$super_native_extensions..platform_impl..platform..os..util..EventExt$GT$::CGEvent::hd37ed3c34aadf60f + 68 22 super_native_extensions 0x1036fd48c super_native_extensions::platform_impl::platform::os::drag::PlatformDragContext::on_momentum_event::he0b4e9c9d45a0ccd + 708 23 super_native_extensions 0x103720b0c super_native_extensions::platform_impl::platform::os::drag::magnify_withevent::$u7b$$u7b$closure$u7d$$u7d$::ha997fe1ee1cb4281 + 116 24 super_native_extensions 0x10371e95c super_native_extensions::platform_impl::platform::os::drag::with_state::h36e8a2ad80f14b3a + 248 25 super_native_extensions 0x1036fef84 super_native_extensions::platform_impl::platform::os::drag::magnify_with_event::h62cd82ba0b634784 + 52 26 AppKit 0x18b98e92c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 764 27 AppKit 0x18b98e45c -[NSWindow(NSEventRouting) sendEvent:] + 284 28 super_nativeextensions 0x1039e0f88 $LT$$LP$A$C$$RP$$u20$as$u20$objc2..encode..EncodeArguments$GT$::invoke::hd0f4995b08bc942f + 88 29 super_native_extensions 0x103945ee0 objc2::runtime::message_receiver::msg_send_primitive::send::h58f1f613bb21eacf + 80 30 super_native_extensions 0x1038c7820 objc2::runtime::message_receiver::MessageReceiver::send_message::h3212ee0addf62a0f + 192 31 super_native_extensions 0x103991278 objc2::__macro_helpers::msg_send::MsgSend::send_message::hd2dcb4fddf1f6e28 + 204 32 super_native_extensions 0x10399af54 icrate::generated::AppKit::NSWindow::NSWindow::sendEvent::h6054799ead4ce5dc + 88 33 super_native_extensions 0x1036fd5c4 super_native_extensions::platform_impl::platform::os::drag::PlatformDragContext::on_momentum_event::he0b4e9c9d45a0ccd + 1020 34 super_native_extensions 0x103720b0c super_native_extensions::platform_impl::platform::os::drag::magnify_withevent::$u7b$$u7b$closure$u7d$$u7d$::ha997fe1ee1cb4281 + 116 35 super_native_extensions 0x10371e95c super_native_extensions::platform_impl::platform::os::drag::with_state::h36e8a2ad80f14b3a + 248 36 super_native_extensions 0x1036fef84 super_native_extensions::platform_impl::platform::os::drag::magnify_with_event::h62cd82ba0b634784 + 52 37 AppKit 0x18b98e92c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 764 38 AppKit 0x18b98e45c -[NSWindow(NSEventRouting) sendEvent:] + 284 39 super_nativeextensions 0x1039e0f88 $LT$$LP$A$C$$RP$$u20$as$u20$objc2..encode..EncodeArguments$GT$::invoke::hd0f4995b08bc942f + 88 40 super_native_extensions 0x103945ee0 objc2::runtime::message_receiver::msg_send_primitive::send::h58f1f613bb21eacf + 80 41 super_native_extensions 0x1038c7820 objc2::runtime::message_receiver::MessageReceiver::send_message::h3212ee0addf62a0f + 192 42 super_native_extensions 0x103991278 objc2::__macro_helpers::msg_send::MsgSend::send_message::hd2dcb4fddf1f6e28 + 204 43 super_native_extensions 0x10399af54 icrate::generated::AppKit::NSWindow::NSWindow::sendEvent::h6054799ead4ce5dc + 88 44 super_native_extensions 0x1036fd5c4 super_native_extensions::platform_impl::platform::os::drag::PlatformDragContext::on_momentum_event::he0b4e9c9d45a0ccd + 1020 45 super_native_extensions 0x103720b0c super_native_extensions::platform_impl::platform::os::drag::magnify_withevent::$u7b$$u7b$closure$u7d$$u7d$::ha997fe1ee1cb4281 + 116 46 super_native_extensions 0x10371e95c super_native_extensions::platform_impl::platform::os::drag::with_state::h36e8a2ad80f14b3a + 248 47 super_native_extensions 0x1036fef84 super_native_extensions::platform_impl::platform::os::drag::magnify_with_event::h62cd82ba0b634784 + 52 48 AppKit 0x18b98e92c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 764 49 AppKit 0x18b98e45c -[NSWindow(NSEventRouting) sendEvent:] + 284 50 super_nativeextensions 0x1039e0f88 $LT$$LP$A$C$$RP$$u20$as$u20$objc2..encode..EncodeArguments$GT$::invoke::hd0f4995b08bc942f + 88 51 super_native_extensions 0x103945ee0 objc2::runtime::message_receiver::msg_send_primitive::send::h58f1f613bb21eacf + 80 52 super_native_extensions 0x1038c7820 objc2::runtime::message_receiver::MessageReceiver::send_message::h3212ee0addf62a0f + 192 53 super_native_extensions 0x103991278 objc2::__macro_helpers::msg_send::MsgSend::send_message::hd2dcb4fddf1f6e28 + 204 54 super_native_extensions 0x10399af54 icrate::generated::AppKit::NSWindow::NSWindow::sendEvent::h6054799ead4ce5dc + 88 55 super_native_extensions 0x1036fd5c4 super_native_extensions::platform_impl::platform::os::drag::PlatformDragContext::on_momentum_event::he0b4e9c9d45a0ccd + 1020 56 super_native_extensions 0x103720b0c super_native_extensions::platform_impl::platform::os::drag::magnify_withevent::$u7b$$u7b$closure$u7d$$u7d$::ha997fe1ee1cb4281 + 116 57 super_native_extensions 0x10371e95c super_native_extensions::platform_impl::platform::os::drag::with_state::h36e8a2ad80f14b3a + 248 58 super_native_extensions 0x1036fef84 super_native_extensions::platform_impl::platform::os::drag::magnify_with_event::h62cd82ba0b634784 + 52 59 AppKit 0x18b98e92c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 764 60 AppKit 0x18b98e45c -[NSWindow(NSEventRouting) sendEvent:] + 284 61 super_nativeextensions 0x1039e0f88 $LT$$LP$A$C$$RP$$u20$as$u20$objc2..encode..EncodeArguments$GT$::invoke::hd0f4995b08bc942f + 88 62 super_native_extensions 0x103945ee0 objc2::runtime::message_receiver::msg_send_primitive::send::h58f1f613bb21eacf + 80 63 super_native_extensions 0x1038c7820 objc2::runtime::message_receiver::MessageReceiver::send_message::h3212ee0addf62a0f + 192 64 super_native_extensions 0x103991278 objc2::__macro_helpers::msg_send::MsgSend::send_message::hd2dcb4fddf1f6e28 + 204 65 super_native_extensions 0x10399af54 icrate::generated::AppKit::NSWindow::NSWindow::sendEvent::h6054799ead4ce5dc + 88 66 super_native_extensions 0x1036fd5c4 super_native_extensions::platform_impl::platform::os::drag::PlatformDragContext::on_momentum_event::he0b4e9c9d45a0ccd + 1020 67 super_native_extensions 0x103720b0c super_native_extensions::platform_impl::platform::os::drag::magnify_withevent::$u7b$$u7b$closure$u7d$$u7d$::ha997fe1ee1cb4281 + 116 68 super_native_extensions 0x10371e95c super_native_extensions::platform_impl::platform::os::drag::with_state::h36e8a2ad80f14b3a + 248 69 super_native_extensions 0x1036fef84 super_native_extensions::platform_impl::platform::os::drag::magnify_with_event::h62cd82ba0b634784 + 52 70 AppKit 0x18b98e92c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 764 71 AppKit 0x18b98e45c -[NSWindow(NSEventRouting) sendEvent:] + 284 72 super_nativeextensions 0x1039e0f88 $LT$$LP$A$C$$RP$$u20$as$u20$objc2..encode..EncodeArguments$GT$::invoke::hd0f4995b08bc942f + 88 73 super_native_extensions 0x103945ee0 objc2::runtime::message_receiver::msg_send_primitive::send::h58f1f613bb21eacf + 80 74 super_native_extensions 0x1038c7820 objc2::runtime::message_receiver::MessageReceiver::send_message::h3212ee0addf62a0f + 192 75 super_native_extensions 0x103991278 objc2::__macro_helpers::msg_send::MsgSend::send_message::hd2dcb4fddf1f6e28 + 204 76 super_native_extensions 0x10399af54 icrate::generated::AppKit::NSWindow::NSWindow::sendEvent::h6054799ead4ce5dc + 88 77 super_native_extensions 0x1036fd5c4 super_native_extensions::platform_impl::platform::os::drag::PlatformDragContext::on_momentum_event::he0b4e9c9d45a0ccd + 1020 78 super_native_extensions 0x103720b0c super_native_extensions::platform_impl::platform::os::drag::magnify_withevent::$u7b$$u7b$closure$u7d$$u7d$::ha997fe1ee1cb4281 + 116 79 super_native_extensions 0x10371e95c super_native_extensions::platform_impl::platform::os::drag::with_state::h36e8a2ad80f14b3a + 248 80 super_native_extensions 0x1036fef84 super_native_extensions::platform_impl::platform::os::drag::magnify_with_event::h62cd82ba0b634784 + 52 81 AppKit 0x18b98e92c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 764 82 AppKit 0x18b98e45c -[NSWindow(NSEventRouting) sendEvent:] + 284


please

BruceChen5683 commented 2 weeks ago

in file drag.rs. Using this code, the crash probability has decreased, but it still crashes, especially when performing a two-finger rotation gesture.

fn on_momentum_event(&self, event: Id<NSEvent>) -> bool {
    let last_event = self.last_momentum_event.borrow().clone();

    // 检查事件时间戳
    if let Some(last_event) = last_event.as_ref() {
        let current_timestamp = unsafe { event.timestamp() };
        let last_timestamp = unsafe { last_event.timestamp() };

        if current_timestamp < last_timestamp {
            eprintln!("Ignoring event: current timestamp {} is less than last timestamp {}", current_timestamp, last_timestamp);
            return false;
        }
    }

    // 获取当前事件和上一个事件的阶段
    let event_phase = unsafe { event.phase() };
    let last_event_phase = last_event
        .map(|e| unsafe { e.phase() })
        .unwrap_or(NSEventPhaseNone);

    eprintln!("Current event phase: {:?}", event_phase);
    eprintln!("Last event phase: {:?}", last_event_phase);

    // 检查事件阶段的有效性
    if (event_phase == NSEventPhaseEnded || event_phase == NSEventPhaseCancelled)
        && (last_event_phase == NSEventPhaseEnded
            || last_event_phase == NSEventPhaseCancelled
            || last_event_phase == NSEventPhaseNone)
    {
        eprintln!("Ignoring event: invalid phase transition from {:?} to {:?}", last_event_phase, event_phase);
        return false;
    }

    // 合成新事件的逻辑
    if event_phase == NSEventPhaseChanged
        && (last_event_phase == NSEventPhaseEnded
            || last_event_phase == NSEventPhaseNone
            || last_event_phase == NSEventPhaseCancelled)
    {
        // 获取 CGEvent 对象
        let cg_event = unsafe { event.CGEvent() };
        if cg_event.is_null() {
            eprintln!("CGEvent is null, cannot proceed");
            return false;
        }

        // 复制 CGEvent 对象
        let copied_event = unsafe { CGEventCreateCopy(cg_event) };
        if copied_event.is_null() {
            eprintln!("Failed to copy CGEvent, cannot proceed");
            return false;
        }

        unsafe {
            CGEventSetIntegerValueField(
                copied_event,
                99, // kCGScrollWheelEventScrollPhase
                NSEventPhaseBegan as i64,
            );

            // 记录日志以便调试
            eprintln!("Synthesizing new event with phase Began");

            // 使用合成的事件
            let synthesized_event = NSEvent::withCGEvent(copied_event);
            CFRelease(copied_event as *mut _);

            if let Some(window) = self.view.window() {
                window.sendEvent(&synthesized_event);

                // 更新 last_momentum_event 以避免重复合成
                self.last_momentum_event.replace(Some(synthesized_event));
            } else {
                eprintln!("Window is None, cannot send event");
                return false;
            }
        }

        // 返回 true,表示事件已处理
        return true;
    }

    // 正常更新最后的动量事件
    self.last_momentum_event.replace(Some(event.clone()));
    true
}
knopp commented 2 weeks ago

I can reproduce the problem, unfortunately it doesn't really make sense. It seems like a bug in Rust 1.78 :-/ Can you confirm that your rustc version is 1.78?

knopp commented 2 weeks ago

Nevermind, I can reproduce it on nightly too.

knopp commented 2 weeks ago

Actually, it looks like a stack overflow 🤦

knopp commented 2 weeks ago

Can you please retry this once 0.8.13 becomes available on pub? Should be in few minutes.

BruceChen5683 commented 2 weeks ago

now 0.8.13 works fine. My Rust version is 1.78. Good job