iOS dealloc方法都做了哪些东西?
2020-08-12 本文已影响0人
柯索
- (void)dealloc {
_objc_rootDealloc(self);
}
_objc_rootDealloc(id obj)
{
ASSERT(obj);
obj->rootDealloc();
}
这段源码在NSObject.mm里面,接着去objc-object.h,objc_object的公共方法

1,先判断是否是TaggedPointer,如果是的话直接返回。因为TaggedPointer它的内存并不存储在堆中,也不需要 malloc 和 free。
2,判断是否指针优化,是否有过弱引用,是否有过动态绑定,是否有过C++析构函数,是否p引用计数器是否过大无法存储在isa中,这些都没有的话直接调free方法释放
3,否则调用object_dispose

3.1判断obj是否存在,调用objc_destructInstance函数

3.1.1 处理c++函数object_cxxDestruct里面再调用 object_cxxDestructFromClass,最后调用(*dtor)(obj);可以理解为调用了C++的析构方法,当前cls释放,superclass释放。

3.1.2 处理 动态绑定_object_remove_assocations

最后调用associations.erase(i)擦除方法,最后release。
4,最后还是free释放