(本文所有内容都是针对64位架构,ARC环境而言)
![](https://img.haomeiwen.com/i2427856/ae502dc135d99c9c.png)
在iOS中,使用引用计数来管理OC对象的内存:
- 一个新创建的对象引用计数默认是1,当引用计数减为0,OC对象就会销毁,释放其占用的内存空间
- 调用retain会使对象的引用计数+1,调用release会使对象的引用计数-1
- ARC会自动帮我们处理引用计数,通过编译器及runtime生成、调用retain或release方法
本文主要通过分析objc源码来窥探release时到底做了哪些事情;
isa结构详解
在探究release方法前,先来了解下isa结构,因为后面相关的地方都会涉及到isa结构;
OC对象都有isa指针指向所属的Class;objc.h中isa定义如下:
struct objc_object {
Class _Nonnull isa OBJC_ISA_AVAILABILITY;
};
现在我们下载objc4源码来探究isa底层结构:
struct objc_class : objc_object {
// Class ISA;
Class superclass;
cache_t cache; // formerly cache pointer and vtable
class_data_bits_t bits;
....
}
// objc-private.h
struct objc_object {
private:
isa_t isa;
public:
// ISA() assumes this is NOT a tagged pointer object
Class ISA();
....
}
可以看到isa数据类型为isa_t
;
在arm64架构之前,isa就是一个普通的指针,存储着Class、Meta-Class对象的内存地址;
从arm64架构开始,对isa进行了优化,isa设计成了一个union共用体结构isa_t,使用位域来存储信息,这样节省内存能存储更多信息;结构体占8字节共8*8=64位,这64位分别存储不同的信息:
![](https://img.haomeiwen.com/i2427856/0650f3888dffb375.png)
isa内部结构各字段含义如下:
![](https://img.haomeiwen.com/i2427856/f1bf7905e336daa2.png)
release方法源码
在源码objc-object.h中可以找到release方法的最终实现objc_object::rootRelease(bool performDealloc, bool handleUnderflow);
rootRelease函数核心代码:
// 未优化过的isa
if (slowpath(!newisa.nonpointer)) {
ClearExclusive(&isa.bits);
if (sideTableLocked) sidetable_unlock();
return sidetable_release(performDealloc);
}
objc_object::sidetable_release(bool performDealloc)
{
// 存储引用计数的SideTable
SideTable& table = SideTables()[this];
// 标记是否调用dealloc 释放对象
bool do_dealloc = false;
table.lock();
// 通过this本身从SideTable的refcnts字典中查询对应的引用计数
RefcountMap::iterator it = table.refcnts.find(this);
if (it == table.refcnts.end()) {
// 引用计数为0
do_dealloc = true;
table.refcnts[this] = SIDE_TABLE_DEALLOCATING;
} else if (it->second < SIDE_TABLE_DEALLOCATING) {
// 引用计数为0
do_dealloc = true;
it->second |= SIDE_TABLE_DEALLOCATING;
} else if (! (it->second & SIDE_TABLE_RC_PINNED)) {
// 引用计数减1
it->second -= SIDE_TABLE_RC_ONE;
}
table.unlock();
if (do_dealloc && performDealloc) {
// 调用dealloc方法 释放对象
((void(*)(objc_object *, SEL))objc_msgSend)(this, SEL_dealloc);
}
return do_dealloc;
}
已优化的isa,前面已经讲过引用计数首先会存储于extra_rc字段;当extra_rc不够用时和为优化的isa一样存储于SideTable中;实现代码和上面大同小异:
size_t
objc_object::sidetable_subExtraRC_nolock(size_t delta_rc)
{
assert(isa.nonpointer);
SideTable& table = SideTables()[this];
RefcountMap::iterator it = table.refcnts.find(this);
if (it == table.refcnts.end() || it->second == 0) {
// Side table retain count is zero. Can't borrow.
return 0;
}
size_t oldRefcnt = it->second;
// isa-side bits should not be set here
assert((oldRefcnt & SIDE_TABLE_DEALLOCATING) == 0);
assert((oldRefcnt & SIDE_TABLE_WEAKLY_REFERENCED) == 0);
size_t newRefcnt = oldRefcnt - (delta_rc << SIDE_TABLE_RC_SHIFT);
assert(oldRefcnt > newRefcnt); // shouldn't underflow
it->second = newRefcnt;
return delta_rc;
}
SideTable不只存储了引用计数,还存储了weak引用,结构如下:
struct SideTable {
spinlock_t slock;
// 引用计数Map,key为对象本身this,value为引用计数
RefcountMap refcnts;
// weak引用Map,key为对象本身this,value为对象指针
weak_table_t weak_table;
}
总的来说,release方法主要是处理引用计数;由于isa有优化(arm64架构)和未优化之分,引用计数存储的位置不同,所以这其中包括对不同情况的判断;但总的逻辑就是引用计数减1,然后判断引用计数是否为0,如果为0则调用dealloc方法,释放对象;
dealloc方法源码
在源码objc-object.h中可以找到dealloc方法的最终实现rootDealloc
inline void
objc_object::rootDealloc()
{
if (isTaggedPointer()) return; // fixme necessary?
// 优化过的isa且没有弱引用、没有关联对象、没有C++析构、引用计数没有存储于sidetable中,则直接释放(直接释放会比较快)
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);
}
}
id
object_dispose(id obj)
{
if (!obj) return nil;
objc_destructInstance(obj);
free(obj);
return nil;
}
核心代码:
void *objc_destructInstance(id obj)
{
if (obj) {
// 判断是否有C++析构 是否有关联对象
bool cxx = obj->hasCxxDtor();
bool assoc = obj->hasAssociatedObjects();
// C++析构相关处理
if (cxx) object_cxxDestruct(obj);
// 删除关联对象
if (assoc) _object_remove_assocations(obj);
obj->clearDeallocating();
}
return obj;
}
objc_object::clearDeallocating()
{
if (slowpath(!isa.nonpointer)) {
// 普通isa指针 未优化过
sidetable_clearDeallocating();
}
else if (slowpath(isa.weakly_referenced || isa.has_sidetable_rc)) {
// 优化过的isa 且有weak refs 或者sidetable有引用计数数据.
clearDeallocating_slow();
}
assert(!sidetable_present());
}
sidetable_clearDeallocating()和clearDeallocating_slow()其实大同小异,主要是清空弱引用指针,清空引用计数数据;
void
objc_object::sidetable_clearDeallocating()
{
SideTable& table = SideTables()[this];
// clear any weak table items
// clear extra retain count and deallocating bit
table.lock();
RefcountMap::iterator it = table.refcnts.find(this);
if (it != table.refcnts.end()) {
if (it->second & SIDE_TABLE_WEAKLY_REFERENCED) {
// 以自身对象this为key 从sidetable中的weak_table找到对应指针并置为nil
weak_clear_no_lock(&table.weak_table, (id)this);
}
// 清除对象引用计数数据
table.refcnts.erase(it);
}
table.unlock();
}
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) {
table.refcnts.erase(this);
}
table.unlock();
}
weak弱引用对象,系统会以对象本身为key,对象指针为value存储于sidetable中的weak_table;当对象引用计数为 0释放时,会从weak_table中找到对应的指针置为nil;因此不同于__unsafe_unretained对象,weak对象是相对安全的不会出现野指针错误;
关联对象原理
前面分析dealloc源码时,我们知道会删除关联对象;在探究是如何删除关联对象前,我们先来了解关联对象被存储在哪里:
查看objc_setAssociatedObject函数源码,对应源码为objc-references.mm中
void _object_set_associative_reference(id object, void *key, id value, uintptr_t policy)函数;
![](https://img.haomeiwen.com/i2427856/c63f201fd5d29346.png)
核心对象为
AssociationsManager、AssociationsHashMap、ObjectAssociationMap、ObjcAssociation
// 管理类
class AssociationsManager {
// associative references: object pointer -> PtrPtrHashMap.
static AssociationsHashMap *_map;
}
// HashMap, key为当前对象value为ObjectAssociationMap
class AssociationsHashMap : public unordered_map<disguised_ptr_t, ObjectAssociationMap *, DisguisedPointerHash, DisguisedPointerEqual, AssociationsHashMapAllocator> {
public:
void *operator new(size_t n) { return ::malloc(n); }
void operator delete(void *ptr) { ::free(ptr); }
};
// HashMap,key为设置的关联对象key,value为ObjcAssociation
class ObjectAssociationMap : public std::map<void *, ObjcAssociation, ObjectPointerLess, ObjectAssociationMapAllocator> {
public:
void *operator new(size_t n) { return ::malloc(n); }
void operator delete(void *ptr) { ::free(ptr); }
};
class ObjcAssociation {
uintptr_t _policy; // 关联对象策略
id _value; // 关联对象值
}
![](https://img.haomeiwen.com/i2427856/373513b1b64918ac.png)
关联对象存储在嵌套的ObjectAssociationMap HashMap结构中,可以通过对象及设置的关联对象key取出这个值;
现在再回过头来看_object_remove_assocations是如何删除关联对象的:
void _object_remove_assocations(id object) {
vector< ObjcAssociation,ObjcAllocator<ObjcAssociation> > elements;
{
AssociationsManager manager;
AssociationsHashMap &associations(manager.associations());
if (associations.size() == 0) return;
// 以对象本身为key
disguised_ptr_t disguised_object = DISGUISE(object);
// 迭代从AssociationsHashMap找到object对应的ObjectAssociationMap
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
// 取得object对应的ObjectAssociationMap
ObjectAssociationMap *refs = i->second;
for (ObjectAssociationMap::iterator j = refs->begin(), end = refs->end(); j != end; ++j) {
// 删除ObjectAssociationMap中所有ObjcAssociation关联对象
elements.push_back(j->second);
}
// remove the secondary table.
delete refs;
// AssociationsHashMap删除object记录
associations.erase(i);
}
}
// the calls to releaseValue() happen outside of the lock.
for_each(elements.begin(), elements.end(), ReleaseValue());
}
总的来说,dealloc方法做的处理:清空引用计数,清除weak弱引用指针,删除关联对象;
网友评论