dealloc on Background Thread

前端 未结 4 1737
北荒
北荒 2021-01-18 22:55

Is it an error to call dealloc on a UIViewController from a background thread? It seems that UITextView (can?) eventually call _

4条回答
  •  不思量自难忘°
    2021-01-18 23:22

    Yes, it is an error to make a UIViewController releasing in a background thread (or queue). In UIKit, dealloc is not thread safe. This is explicitly described in Apple's TN2109 doc:

    When a secondary thread retains the target object, you have to ensure that the thread releases that reference before the main thread releases its last reference to the object. If you don't do this, the last reference to the object is released by the secondary thread, which means that the object's -dealloc method runs on that secondary thread. This is problematic if the object's -dealloc method does things that are not safe to do on a secondary thread, something that's common for UIKit objects like a view controller.

提交回复
热议问题