OC对象的内存销毁步骤

来源:互联网 发布:java校验身份证号 编辑:程序博客网 时间:2024/05/16 07:37

General Information

We take advantage of the documented Deallocation Timeline (WWDC 2011, Session 322, 36:22).
1. -release to zero
* Object is now deallocating and will die.
* New __weak references are not allowed, and will get nil.
* [self dealloc] is called
2. Subclass -dealloc
* bottom-most subclass -dealloc is called
* Non-ARC code manually releases iVars
* Walk the super-class chain calling -dealloc
3. NSObject -dealloc
* Simply calls the ObjC runtime object_dispose()
4. object_dispose()
* Call destructors for C++ iVars
* Call -release for ARC iVars
* Erase associated references
* Erase __weak references
* Call free()


 1. 调用 -release :引用计数变为零
     * 对象正在被销毁,生命周期即将结束.
     * 不能再有新的 __weak 弱引用, 否则将指向 nil.
     * 调用 [self dealloc] 
 2. 父类 调用 -dealloc
     * 继承关系中最底层的父类 在调用 -dealloc
     * 如果是 MRC 代码 则会手动释放实例变量们(iVars)
     * 继承关系中每一层的父类 都在调用 -dealloc
 3. NSObject 调 -dealloc
     * 只做一件事:调用 Objective-C runtime 中的 object_dispose() 方法
 4. 调用 object_dispose()
     * 为 C++ 的实例变量们(iVars)调用 destructors 
     * 为 ARC 状态下的 实例变量们(iVars) 调用 -release 
     * 解除所有使用 runtime Associate方法关联的对象
     * 解除所有 __weak 引用
     * 调用 free()




0 0
原创粉丝点击