1、对象销毁时会调用dealloc方法,方法内部调用_objc_rootDealloc函数
- (void)dealloc {
_objc_rootDealloc(self);
}
2、_objc_rootDealloc函数内部调用了对象的rootDealloc函数
void
_objc_rootDealloc(id obj)
{
assert(obj);
obj->rootDealloc();
}
3、rootDealloc函数释放对象,分两种情况快速销毁和正常销毁
快速销毁需满足
1不是tagpointer对象,
2没有弱引用,
3没有关联对象,
4没有c++析构函数,没有引用计数表
inline void
objc_object::rootDealloc()
{
if (isTaggedPointer()) return; // fixme necessary?
//先判断能不能快速销毁对象,不是tagpointer对象,没有弱引用,没有关联对象,没有c++析构函数,没有引用计数表
if (fastpath(isa.nonpointer &&
!isa.weakly_referenced &&
!isa.has_assoc &&
!isa.has_cxx_dtor &&
!isa.has_sidetable_rc))
{
assert(!sidetable_present());
free(this);
}
else {
//否则走正常销毁路径
object_dispose((id)this);
}
}
4、object_dispose函数
id
object_dispose(id obj)
{
if (!obj) return nil;
objc_destructInstance(obj);
free(obj);
return nil;
}
void *objc_destructInstance(id obj)
{
if (obj) {
// Read all of the flags at once for performance.
bool cxx = obj->hasCxxDtor();//是否有c++析构函数
bool assoc = obj->hasAssociatedObjects();//释放有关联对象
// This order is important.
if (cxx) object_cxxDestruct(obj);//如果有c++析构函数,就去销毁c++
if (assoc) _object_remove_assocations(obj);//如果有关联对象就释放关联对象
obj->clearDeallocating();//去释放对象
}
return obj;
}
5、释放关联对象
void _object_remove_assocations(id object) {
vector< ObjcAssociation,ObjcAllocator<ObjcAssociation> > elements;
{
AssociationsManager manager;
AssociationsHashMap &associations(manager.associations());
if (associations.size() == 0) return;
disguised_ptr_t disguised_object = DISGUISE(object);
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
// copy all of the associations that need to be removed.
ObjectAssociationMap *refs = i->second;
for (ObjectAssociationMap::iterator j = refs->begin(), end = refs->end(); j != end; ++j) {
elements.push_back(j->second);
}
// remove the secondary table.
delete refs;
associations.erase(i);
}
}
// the calls to releaseValue() happen outside of the lock.
for_each(elements.begin(), elements.end(), ReleaseValue());
}
6、释放C++对象
void object_cxxDestruct(id obj)
{
if (!obj) return;
if (obj->isTaggedPointer()) return;
object_cxxDestructFromClass(obj, obj->ISA());
}
static void object_cxxDestructFromClass(id obj, Class cls)
{
void (*dtor)(id);
// Call cls's dtor first, then superclasses's dtors.
for ( ; cls; cls = cls->superclass) {
if (!cls->hasCxxDtor()) return;
dtor = (void(*)(id))
lookupMethodInClassAndLoadCache(cls, SEL_cxx_destruct);
if (dtor != (void(*)(id))_objc_msgForward_impcache) {
if (PrintCxxCtors) {
_objc_inform("CXX: calling C++ destructors for class %s",
cls->nameForLogging());
}
(*dtor)(obj);
}
}
}
IMP lookupMethodInClassAndLoadCache(Class cls, SEL sel)
{
Method meth;
IMP imp;
// fixme this is incomplete - no resolver, +initialize -
// but it's only used for .cxx_construct/destruct so we don't care
assert(sel == SEL_cxx_construct || sel == SEL_cxx_destruct);
// Search cache first.
imp = cache_getImp(cls, sel);
if (imp) return imp;
// Cache miss. Search method list.
mutex_locker_t lock(runtimeLock);
meth = getMethodNoSuper_nolock(cls, sel);
if (meth) {
// Hit in method list. Cache it.
cache_fill(cls, sel, meth->imp, nil);
return meth->imp;
} else {
// Miss in method list. Cache objc_msgForward.
cache_fill(cls, sel, _objc_msgForward_impcache, nil);
return _objc_msgForward_impcache;
}
}
7、最后销毁OC对象调用clearDeallocating函数,
inline void
objc_object::clearDeallocating()
{
if (slowpath(!isa.nonpointer)) {
// Slow path for raw pointer isa.
sidetable_clearDeallocating();//清除引用计数表
}
else if (slowpath(isa.weakly_referenced || isa.has_sidetable_rc)) {
// Slow path for non-pointer isa with weak refs and/or side table data.
clearDeallocating_slow();//走这个方法
}
assert(!sidetable_present());
}
1,释放弱引用对象
2,引用计数清0
NEVER_INLINE void
objc_object::clearDeallocating_slow()
{
assert(isa.nonpointer && (isa.weakly_referenced || isa.has_sidetable_rc));
SideTable& table = SideTables()[this];
table.lock();
if (isa.weakly_referenced) {
//释放弱引用
weak_clear_no_lock(&table.weak_table, (id)this);
}
if (isa.has_sidetable_rc) {
//清0引用计数
table.refcnts.erase(this);
}
table.unlock();
}