iOS開發——__ZL17_WebTryThreadLockb

sdk 中涉及到 UI 操作的時候,一定要注意線程問題!一定要注意線程問題!一定要注意線程問題!

從最初開始學習 iOS 的時候,我們就被告知 UI 操作一定要放在主線程進行,這是因為 UIKit 的方法不是線程安全的,保證線程安全需要極大的開銷。

試想一下幾種場景:

  • 兩個線程同時設置同一個背景圖片,則很有可能因為當前圖片被釋放了兩次而導致應用崩潰;
  • 兩個線程同時設置同一個 UIView 的背景色,則很有可能渲染顯示的是顏色 A,而此時在 UIView 邏輯樹上的背景顏色屬性為 B;
  • 兩個線程同時操作 View 的樹形結構:在線程 A 中 for 循環遍歷并操作當前 View 的所有 subView,而此時線程 B 中將某個 subView 直接刪除,這就導致了錯亂,甚至導致應用崩潰。

出了什么問題?

最近 hybrid sdk 收尾,偶然發現線上有一類 crash 最近兩個版本穩步上升,而且可以肯定的是我負責的 sdk 提供的 web 容器導致的,__ZL17_WebTryThreadLockb 是函數調用棧最后調用的 api。

crash 線程調用棧詳細如下:

Thread 17 Crashed:
0   WebCore                              __ZL17_WebTryThreadLockb
1   WebCore                              _WebThreadLock
2   UIKit                                -[UIWebBrowserView resignFirstResponder]
3   UIKit                                -[UIResponder _resignIfContainsFirstResponder]
4   UIKit                                -[UIView(Hierarchy) _willMoveToWindow:]
5   UIKit                                -[UIView(Internal) _addSubview:positioned:relativeTo:]
6   UIKit                                ___53-[_UINavigationParallaxTransition animateTransition:]_block_invoke_2
7   UIKit                                +[UIView(Animation) performWithoutAnimation:]
8   UIKit                                ___53-[_UINavigationParallaxTransition animateTransition:]_block_invoke
9   UIKit                                +[UIView(Internal) _performBlockDelayingTriggeringResponderEvents:]
10  UIKit                                -[_UINavigationParallaxTransition animateTransition:]
11  UIKit                                -[UINavigationController _startCustomTransition:]
12  UIKit                                -[UINavigationController _startDeferredTransitionIfNeeded:]
13  UIKit                                -[UINavigationController __viewWillLayoutSubviews]
14  UIKit                                -[UILayoutContainerView layoutSubviews]
15  UIKit                                -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
16  QuartzCore                           -[CALayer layoutSublayers]
17  QuartzCore                           __ZN2CA5Layer16layout_if_neededEPNS_11TransactionE
18  QuartzCore                           __ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE
19  QuartzCore                           __ZN2CA7Context18commit_transactionEPNS_11TransactionE
20  QuartzCore                           __ZN2CA11Transaction6commitEv
21  QuartzCore                           __ZN2CA11Transaction14release_threadEPv
22  libsystem_pthread.dylib              __pthread_tsd_cleanup
23  libsystem_pthread.dylib              __pthread_exit
24  libsystem_pthread.dylib              __pthread_wqthread
25  libsystem_pthread.dylib              _start_wqthread

原因是什么?

Stack Overflow 上的問題和解答貼出兩個大家一起參考:

app-crash-no-idea-why
ios-webtrythreadlock-crash

根本原因:Tried to obtain the web lock from a thread other than the main thread or the web thread.

問題定位

ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();
    if (authStatus == kABAuthorizationStatusNotDetermined)
    {
        //獲取授權
        ABAddressBookRef addressBook = ABAddressBookCreate();
        ABAddressBookRequestAccessWithCompletion( addressBook, ^(bool granted, CFErrorRef error) {
            if (granted)
            {
                [self openContact];
            }
            else
            {
                [self showAlertView];
            }
        });
    }

之前草率的確定為上述代碼問題,上述代碼確實存在兩個問題

  1. 內存泄漏
  2. 非主線程調用 UI 操作

但重新看 crash 調用棧,確定并不會是 AddressBook 導致,而真實原因已無從考證。
更新文章,留作后人查閱。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容