[UINavigationController retain]: message sent to deallocated instance

前端 未结 3 1798
礼貌的吻别
礼貌的吻别 2021-01-20 11:07

My application crashes when simulating Memory warning in simulator with error:

[UINavigationController retain]: message sent to deallocated instance<

3条回答
  •  粉色の甜心
    2021-01-20 11:58

    Quickfix: insert assert([NSThread isMainThread]); to various places in your code where you access appDelegate.window.rootViewController. This should be done for write- and for read-accesses to the property! This will reveal the culprit. appDelegate.window.rootViewController must not be accessed from any other thread than the main thread.

    Generally, there are these reasons why this may happen:

    1. You are using __unsafe_unretained variables.
    2. You are using an unsafe_unretained property.
    3. You are using non-ARC
    4. You are accessing the same variable from different threads at the same time
    5. You are accessing the same nonatomic, non-weak property from different threads at the same time

    The fix for 1 and 2 is simple: Just don't use unsafe_unretained anymore.

    The fix for 3 is: use ARC instead.

    The fix for 4 and 5: use atomic properties instead, or synchronize access to your iVars. (Note that you must not access iVars from atomic properties directly as this breaks the atomicity.) Alternatively, use the property only from one thread, e.g. only from the main thread.

    In your example, I assume that issue #5 applies. The culprit should be some non-main-thread accessing rootViewController from UIWindow.

提交回复
热议问题