Closed BruceChen5683 closed 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
}
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?
Nevermind, I can reproduce it on nightly too.
Actually, it looks like a stack overflow 🤦
Can you please retry this once 0.8.13 becomes available on pub? Should be in few minutes.
now 0.8.13 works fine. My Rust version is 1.78. Good job
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
please