使用 Hammer.js 时移动 Safari 崩溃

Posted

技术标签:

【中文标题】使用 Hammer.js 时移动 Safari 崩溃【英文标题】:Mobile Safari crashes when using Hammer.js 【发布时间】:2014-02-05 18:06:14 【问题描述】:

我正在开发一款适用于 iPad 的 javascript 多人游戏。我将 Smartfox Server 用于多人游戏,并将 Hammer.js 用于触摸事件。

我遇到了一个问题,当我在游戏的某个特定部分使用 Hammer 事件时,Mobile Safari 会崩溃。它在 PC 上运行良好,但在 iPad 上却导致崩溃。

这是我的代码发生的部分:

this.startButton.hammer().on("tap", this.handleStartClicked);

我在“handleStartClicked”函数中所做的只是从输入框中获取用户名和密码,将它们保存到本地存储(如果可用),然后调用 Smartfox“连接”函数,它将用户连接到 Smartfox 并开始登录过程。这是函数:

onStartClicked:function(e)
  if(this.nameInput.val() == "" || this.pwInput.val() == "") return;

  this.startButton.unbind("click", this.onStartClickBind);

  if(window.localStorage != null && window['localStorage'] != null)
    window.localStorage.username = this.nameInput.val();
    window.localStorage.password = this.pwInput.val();
  

  this.sfs.connect();

如果我单击开始按钮,Mobile Safari 大约有 80% 的时间会崩溃。即使它没有在点击时立即崩溃,它通常也会在游戏后期的某个时间点崩溃。但是,如果我从不点击开始按钮(例如,如果我只是在没有触摸交互的情况下自动启动用户),则不会发生崩溃——即使在游戏的后期也不会发生。我什至尝试完全删除开始按钮,只是将触摸侦听器添加到整个文档正文中,它仍然崩溃,所以它似乎与按钮没有任何关系(它只是一个普通的 html 按钮,包裹在jQuery 对象)。

奇怪的是,如果我将 Hammer.js 事件更改为常规 jQuery 事件:

this.startButton.bind("click", this.onStartClickBind);

Mobile Safari 永远不会崩溃。不是在开始点击...不是在游戏中的任何时候。它似乎可以解决所有问题。另一个奇怪的事情是,我在我的其余代码中都使用了 Hammer 事件,但它似乎是一个问题的唯一地方是在这个处理程序上。我不知道这个处理程序在游戏中与其他处理程序有什么不同,除非崩溃与 Mobile Safari、Hammer.js 和 Smartfox 之间的交互有关。我在游戏中的其他 Hammer 处理程序上向 Smartfox 发送调用,但它们不会使 Safari 崩溃,所以这似乎不太可能。

另外,我在下面有 iPad 的崩溃日志,如果这能进一步说明问题:

Incident Identifier: 540C3060-271F-468E-9DF8-DA641DACA406
CrashReporter Key:   a5c833f7168137e99d3c8aa6a4d35804a584b7b4
Hardware Model:      iPad3,1
Process:             Web [210]
Path:                /var/mobile/Applications/327316BD-8A50-4245-B728-  
E6504C19A76D/Web.app/Web
Identifier:          com.apple.webapp
Version:             1.0 (1.0)
Code Type:           ARM (Native)
Parent Process:      launchd [1]

Date/Time:           2014-01-13 14:01:50.112 -0800
OS Version:          ios 7.0.4 (11B554a)
Report Version:      104

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x000000000000defe
Triggered by Thread:  4

Thread 0:
0   JavaScriptCore                  0x306a61f2 JSC::JSValue::get(JSC::ExecState*,   
JSC::PropertyName, JSC::PropertySlot&) const + 678
1   JavaScriptCore                  0x3069a5de llint_slow_path_get_by_id + 162
2   JavaScriptCore                  0x3069fbaa llint_op_get_by_id + 132
3   JavaScriptCore                  0x306ab434 JSC::call(JSC::ExecState*, JSC::JSValue,  
JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&) + 52
4   WebCore                         0x37787ad2  
WebCore::JSMainThreadExecState::call(JSC::ExecState*, JSC::JSValue, JSC::CallType,   
JSC::CallData const&, JSC::JSValue, JSC::ArgList const&) + 142
5   WebCore                         0x379707bc  
WebCore::JSEventListener::handleEvent(WebCore::ScriptExecutionContext*,   
WebCore::Event*) + 516
6   WebCore                         0x3792edca     
WebCore::EventTarget::fireEventListeners(WebCore::Event*, WebCore::EventTargetData*,  
WTF::Vector<WebCore::RegisteredEventListener, 1ul, WTF::CrashOnOverflow>&) + 482
7   WebCore                         0x3782ea2a   
WebCore::EventTarget::fireEventListeners(WebCore::Event*) + 174
8   WebCore                         0x379e7f24 
WebCore::EventTarget::dispatchEvent(WTF::PassRefPtr<WebCore::Event>) + 60
9   WebCore                         0x380e56e0 
WebCore::WebSocket::didReceiveMessage(WTF::String const&) + 68
10  WebCore                         0x380e7ef0  
WebCore::WebSocketChannel::processFrame() + 2292
11  WebCore                         0x380e71a0  
WebCore::WebSocketChannel::processBuffer() + 80
12  WebCore                         0x380e713c 
WebCore::WebSocketChannel::didReceiveSocketStreamData(WebCore::SocketStreamHandle*, 
char const*, int) + 232
13  WebCore                         0x37fdc5c4    
WebCore::SocketStreamHandle::readStreamCallback(unsigned long) + 236
14  CoreFoundation                  0x2f68f918 _signalEventSync + 116
15  CoreFoundation                  0x2f698ee8 _cfstream_solo_signalEventSync + 192
16  CoreFoundation                  0x2f68f5c8 _CFStreamSignalEvent + 324
17  CFNetwork                       0x2f2f277a  
CoreReadStreamCFStreamSupport::coreStreamReadEvent(__CoreReadStream*, unsigned long) +   
70
18  CFNetwork                       0x2f2f2726    
CoreReadStreamClient::coreStreamEventsAvailable(unsigned long) + 34
19  CFNetwork                       0x2f399110 
CoreStreamBase::_callClientNow(CoreStreamClient*) + 40
20  CFNetwork                       0x2f2f24ca  
CoreStreamBase::_streamSetEventAndScheduleDelivery(unsigned long, unsigned char) + 118
21  CFNetwork                       0x2f2f2376 
CoreStreamBase::_streamInterface_SignalEvent(unsigned long, CFStreamError const*) + 30
22  CFNetwork                       0x2f2f2312  
SocketStream::dispatchSignalFromSocketCallbackUnlocked(SocketStreamSignalHolder*) + 50
23  CFNetwork                       0x2f2f1db4 
SocketStream::socketCallback(__CFSocket*, unsigned long, __CFData const*, void const*)   
+ 140
24  CFNetwork                       0x2f2f1cee  
SocketStream::_SocketCallBack_stream(__CFSocket*, unsigned long, __CFData const*, void    
const*, void*) + 54
25  CoreFoundation                  0x2f6cb334 __CFSocketPerformV0 + 576
26  CoreFoundation                  0x2f6c9180 
__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 12
27  CoreFoundation                  0x2f6c864e __CFRunLoopDoSources0 + 202
28  CoreFoundation                  0x2f6c6e42 __CFRunLoopRun + 618
29  CoreFoundation                  0x2f631c22 CFRunLoopRunSpecific + 518
30  CoreFoundation                  0x2f631a06 CFRunLoopRunInMode + 102
31  GraphicsServices                0x3432527e GSEventRunModal + 134
32  UIKit                           0x31ed5044 UIApplicationMain + 1132
33  Web                             0x000821f6 0x81000 + 4598
34  libdyld.dylib                   0x3a374ab4 start + 0

Thread 1:
0   libsystem_kernel.dylib          0x3a41883c kevent64 + 24
1   libdispatch.dylib               0x3a359210 _dispatch_mgr_invoke + 228
2   libdispatch.dylib               0x3a358f96 _dispatch_mgr_thread$VARIANT$mp + 34

Thread 2:
0   libsystem_kernel.dylib          0x3a42bc7c __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x3a491dc6 _pthread_wqthread + 306
2   libsystem_pthread.dylib         0x3a491c80 start_wqthread + 4

Thread 3:
0   libsystem_kernel.dylib          0x3a42bc7c __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x3a491dc6 _pthread_wqthread + 306
2   libsystem_pthread.dylib         0x3a491c80 start_wqthread + 4

Thread 4 name:  WebThread
Thread 4 Crashed:
0   CoreFoundation                  0x2f62e894 CFRelease + 1132
1   WebCore                         0x37814010 WebCore::stopSharedTimer() + 28
2   WebCore                         0x37a58150  
WebCore::DeferrableOneShotTimer<WebCore::CachedResource>::fired() + 40
3   WebCore                         0x377863fc  
WebCore::ThreadTimers::sharedTimerFiredInternal() + 132
4   WebCore                         0x3778634e WebCore::timerFired(__CFRunLoopTimer*,  
void*) + 22
5   CoreFoundation                  0x2f6c90dc 
__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 12
6   CoreFoundation                  0x2f6c8cf2 __CFRunLoopDoTimer + 778
7   CoreFoundation                  0x2f6c708e __CFRunLoopRun + 1206
8   CoreFoundation                  0x2f631c22 CFRunLoopRunSpecific + 518
9   CoreFoundation                  0x2f631a06 CFRunLoopRunInMode + 102
10  WebCore                         0x37814266 RunWebThread(void*) + 414
11  libsystem_pthread.dylib         0x3a493c1a _pthread_body + 138
12  libsystem_pthread.dylib         0x3a493b8a _pthread_start + 98
13  libsystem_pthread.dylib         0x3a491c8c thread_start + 4

Thread 5 name:  com.apple.NSURLConnectionLoader
Thread 5:
0   libsystem_kernel.dylib          0x3a418a8c mach_msg_trap + 20
1   libsystem_kernel.dylib          0x3a418888 mach_msg + 44
2   CoreFoundation                  0x2f6c87be __CFRunLoopServiceMachPort + 150
3   CoreFoundation                  0x2f6c6ee4 __CFRunLoopRun + 780
4   CoreFoundation                  0x2f631c22 CFRunLoopRunSpecific + 518
5   CoreFoundation                  0x2f631a06 CFRunLoopRunInMode + 102
6   Foundation                      0x3006c2f2 +[NSURLConnection(Loader) 
_resourceLoadLoop:] + 314
7   Foundation                      0x300e1c82 __NSThread__main__ + 1058
8   libsystem_pthread.dylib         0x3a493c1a _pthread_body + 138
9   libsystem_pthread.dylib         0x3a493b8a _pthread_start + 98
10  libsystem_pthread.dylib         0x3a491c8c thread_start + 4

Thread 6:
0   libsystem_kernel.dylib          0x3a418a8c mach_msg_trap + 20
1   libsystem_kernel.dylib          0x3a418888 mach_msg + 44
2   CoreFoundation                  0x2f6c87be __CFRunLoopServiceMachPort + 150
3   CoreFoundation                  0x2f6c6ee4 __CFRunLoopRun + 780
4   CoreFoundation                  0x2f631c22 CFRunLoopRunSpecific + 518
5   CoreFoundation                  0x2f631a06 CFRunLoopRunInMode + 102
6   libAVFAudio.dylib               0x2e6b4584 GenericRunLoopThread::Entry(void*) + 124
7   libAVFAudio.dylib               0x2e6a899c CAPThread::Entry(CAPThread*) + 176
8   libsystem_pthread.dylib         0x3a493c1a _pthread_body + 138
9   libsystem_pthread.dylib         0x3a493b8a _pthread_start + 98
10  libsystem_pthread.dylib         0x3a491c8c thread_start + 4

Thread 7:
0   libsystem_kernel.dylib          0x3a42bc7c __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x3a491dc6 _pthread_wqthread + 306
2   libsystem_pthread.dylib         0x3a491c80 start_wqthread + 4

Thread 8:
0   libsystem_kernel.dylib          0x3a42bc7c __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x3a491dc6 _pthread_wqthread + 306
2   libsystem_pthread.dylib         0x3a491c80 start_wqthread + 4

Thread 9 name:  JavaScriptCore::BlockFree
Thread 9:
0   libsystem_kernel.dylib          0x3a42af38 __psynch_cvwait + 24
1   libsystem_pthread.dylib         0x3a493224 _pthread_cond_wait + 536
2   libsystem_pthread.dylib         0x3a494040 pthread_cond_timedwait + 40
3   JavaScriptCore                  0x30656eb0  
WTF::ThreadCondition::timedWait(WTF::Mutex&, double) + 104
4   JavaScriptCore                  0x30656cdc   
JSC::BlockAllocator::blockFreeingThreadMain() + 88
5   JavaScriptCore                  0x306543a0 WTF::wtfThreadEntryPoint(void*) + 12
6   libsystem_pthread.dylib         0x3a493c1a _pthread_body + 138
7   libsystem_pthread.dylib         0x3a493b8a _pthread_start + 98
8   libsystem_pthread.dylib         0x3a491c8c thread_start + 4

Thread 10 name:  JavaScriptCore::Marking
Thread 10:
0   libsystem_kernel.dylib          0x3a42af38 __psynch_cvwait + 24
1   libsystem_pthread.dylib         0x3a493224 _pthread_cond_wait + 536
2   libsystem_pthread.dylib         0x3a494000 pthread_cond_wait + 36
3   JavaScriptCore                  0x307f2236 JSC::GCThread::waitForNextPhase() + 74
4   JavaScriptCore                  0x307f2290 JSC::GCThread::gcThreadMain() + 48
5   JavaScriptCore                  0x306543a0 WTF::wtfThreadEntryPoint(void*) + 12
6   libsystem_pthread.dylib         0x3a493c1a _pthread_body + 138
7   libsystem_pthread.dylib         0x3a493b8a _pthread_start + 98
8   libsystem_pthread.dylib         0x3a491c8c thread_start + 4

Thread 11 name:  WebCore: CFNetwork Loader
Thread 11:
0   libsystem_kernel.dylib          0x3a418a8c mach_msg_trap + 20
1   libsystem_kernel.dylib          0x3a418888 mach_msg + 44
2   CoreFoundation                  0x2f6c87be __CFRunLoopServiceMachPort + 150
3   CoreFoundation                  0x2f6c6ee4 __CFRunLoopRun + 780
4   CoreFoundation                  0x2f631c22 CFRunLoopRunSpecific + 518
5   CoreFoundation                  0x2f631a06 CFRunLoopRunInMode + 102
6   WebCore                         0x3785cd5a WebCore::runLoaderThread(void*) + 250
7   JavaScriptCore                  0x306543a0 WTF::wtfThreadEntryPoint(void*) + 12
8   libsystem_pthread.dylib         0x3a493c1a _pthread_body + 138
9   libsystem_pthread.dylib         0x3a493b8a _pthread_start + 98
10  libsystem_pthread.dylib         0x3a491c8c thread_start + 4

Thread 12 name:  com.apple.CFSocket.private
Thread 12:
0   libsystem_kernel.dylib          0x3a42b440 select$DARWIN_EXTSN + 20
1   CoreFoundation                  0x2f6cc680 __CFSocketManager + 480
2   libsystem_pthread.dylib         0x3a493c1a _pthread_body + 138
3   libsystem_pthread.dylib         0x3a493b8a _pthread_start + 98
4   libsystem_pthread.dylib         0x3a491c8c thread_start + 4

Thread 13:
0   libsystem_kernel.dylib          0x3a42bc7c __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x3a491dc6 _pthread_wqthread + 306
2   libsystem_pthread.dylib         0x3a491c80 start_wqthread + 4

Thread 14:
0   libsystem_kernel.dylib          0x3a42bc7c __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x3a491dc6 _pthread_wqthread + 306
2   libsystem_pthread.dylib         0x3a491c80 start_wqthread + 4

Thread 15 name:  WebCore: LocalStorage
Thread 15:
0   libsystem_kernel.dylib          0x3a42af38 __psynch_cvwait + 24
1   libsystem_pthread.dylib         0x3a493224 _pthread_cond_wait + 536
2   libsystem_pthread.dylib         0x3a494000 pthread_cond_wait + 36
3   JavaScriptCore                  0x30656e82 
WTF::ThreadCondition::timedWait(WTF::Mutex&, double) + 58
4   WebCore                         0x379473a8 WTF::PassOwnPtr<WTF::Function<void ()> >   
WTF::MessageQueue<WTF::Function<void ()> >::waitForMessageFilteredWithTimeout<bool  
(WTF::Function<void ()>*)>(WTF::MessageQueueWaitResult&, bool (&)(WTF::Function<void   
()>*), double) + 104
5   WebCore                         0x3794732e   
WebCore::StorageThread::threadEntryPoint() + 162
6   JavaScriptCore                  0x306543a0 WTF::wtfThreadEntryPoint(void*) + 12
7   libsystem_pthread.dylib         0x3a493c1a _pthread_body + 138
8   libsystem_pthread.dylib         0x3a493b8a _pthread_start + 98
9   libsystem_pthread.dylib         0x3a491c8c thread_start + 4

Thread 16 name:  WebCore: LocalStorage
Thread 16:
0   libsystem_kernel.dylib          0x3a42af38 __psynch_cvwait + 24
1   libsystem_pthread.dylib         0x3a493224 _pthread_cond_wait + 536
2   libsystem_pthread.dylib         0x3a494000 pthread_cond_wait + 36
3   JavaScriptCore                  0x30656e82  
WTF::ThreadCondition::timedWait(WTF::Mutex&, double) + 58
4   WebCore                         0x379473a8 WTF::PassOwnPtr<WTF::Function<void ()> >    
WTF::MessageQueue<WTF::Function<void ()> >::waitForMessageFilteredWithTimeout<bool  
(WTF::Function<void ()>*)>(WTF::MessageQueueWaitResult&, bool (&)(WTF::Function<void  
()>*), double) + 104
5   WebCore                         0x3794732e  
WebCore::StorageThread::threadEntryPoint() + 162
6   JavaScriptCore                  0x306543a0 WTF::wtfThreadEntryPoint(void*) + 12
7   libsystem_pthread.dylib         0x3a493c1a _pthread_body + 138`enter code here`
8   libsystem_pthread.dylib         0x3a493b8a _pthread_start + 98
9   libsystem_pthread.dylib         0x3a491c8c thread_start + 4

Thread 4 crashed with ARM Thread State (32-bit):
r0: 0x00000000    r1: 0x7e190594      r2: 0x155bc514      r3: 0x00000001
r4: 0x3bfa51ec    r5: 0x0275793c      r6: 0x00000000      r7: 0x028ee074
r8: 0x155bc568    r9: 0x03002c80     r10: 0x028ee128     r11: 0x155bc510
ip: 0x3bee191c    sp: 0x028ee034      lr: 0x37814015      pc: 0x2f62e894
cpsr: 0x60000030

任何见解都会非常有帮助。谢谢!

【问题讨论】:

可能是越狱 ios 的一种方法:D 真的向 Hammer 开发者寻求帮助 【参考方案1】:

来自崩溃日志:

Triggered by Thread:  4

...

Thread 4 name:  WebThread
Thread 4 Crashed:
0   CoreFoundation                  0x2f62e894 CFRelease + 1132
1   WebCore                         0x37814010 WebCore::stopSharedTimer() + 28
2   WebCore                         0x37a58150 WebCore::DeferrableOneShotTimer<WebCore::CachedResource>::fired() + 40

看起来CFRelease 失败了,可能是因为发布了一些已经发布的东西(尽管CFRelease 的偏移量看起来非常高)。无论如何,这确实必须是 WebKit 中的一个错误。您是否尝试过更新到更高版本的 iOS?

我还注意到,在您的代码示例中,您是从“点击”解除绑定,而不是从“点击”解除绑定。不确定这是否是故意的(可能只是复制粘贴问题,因为我还注意到您将“点击”附加到handleStartClicked,但您粘贴的功能是onStartClicked)。

【讨论】:

【参考方案2】:

我在移动 Safari 和 HammerJS 中遇到了类似的问题,是使用 localStorage 导致了随机崩溃。我仍然不确定它为什么会发生,但是一旦我们将本地存储抽象到其他地方并将 eventemitter 与hammerjs 侦听器一起使用,它就会起作用。

Hammer(this.startButton).on('press tap', function()
    that.emit('handleClickStart', this.sfs);


Controller.prototype.__proto__ = EventEmitter2.prototype; 

Controller.prototype.handleClickStart = function(sfs)
    if (!credential) return;

    storageLib.set('credentials', credentials);

    sfs.connect();
;

var storageLib = 
    localStorage: localStorage,
    set: function(key, val) 
        if(val instanceof Object) val = JSON.stringify(val);
        this.localStorage.setItem(key, val);
        return this.isSet(key);
    ,
    isSet: function(key)
        var item = this.localStorage.getItem(key);
        if(item) return true;
        else return false;
    
;

【讨论】:

以上是关于使用 Hammer.js 时移动 Safari 崩溃的主要内容,如果未能解决你的问题,请参考以下文章

移动端手势事件 hammer.JS插件

认识hammer.js

Hammer.js 中文教程(有 demo)

使用jquery.hammer.js时如何返回X和Y坐标

Hammer.js分析——input.js

如何在特定视口上设置触摸手势/hammer.js 处于活动状态?