前言
在iOS開發過程中,會經常使用到一個修飾詞“weak”,使用場景大家都比較清晰,用于一些對象相互引用的時候,避免出現強強引用,對象不能被釋放,出現內存泄露的問題。
weak 關鍵字的作用弱引用,所引用對象的計數器不會加一,并在引用對象被釋放的時候自動被設置為 nil。
weak底層原理
1.weak編譯解析
首先需要看一下weak編譯之后具體出現什么樣的變化,通過Clang的方法把weak編譯成C++
int main(){
NSObject *obj = [[NSObject alloc] init];
id __weak obj1 = obj;
}
編譯之后的weak,通過objc_ownership(weak)實現weak方法,objc_ownership字面意思是:獲得對象的所有權,是對對象weak的初始化的一個操作。
在使用clang編譯過程中會報錯誤,使用下方的方法編碼編譯出現error
clang -rewrite-objc -fobjc-arc -stdlib=libc++ -mmacosx-version-min=10.7 -fobjc-runtime=macosx-10.7 -Wno-deprecated-declarations main.m
int main(){
NSObject *obj = ((NSObject *(*)(id, SEL))(void *)objc_msgSend)((id)((NSObject *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("NSObject"), sel_registerName("alloc")), sel_registerName("init"));
id __attribute__((objc_ownership(weak))) obj1 = obj;
}
2.weak的實現原理
第一、通過weak編譯解析,可以看出來weak通過runtime初始化的并維護的;
第二、weak和strong都是Object-C的修飾詞,而strong是通過runtime維護的一個自動計數表結構。
綜上:weak是有Runtime維護的weak表。
在runtime源碼中,可以找到'objc-weak.h'和‘objc-weak.mm’文件,并且在objc-weak.h文件中關于定義weak表的結構體以及相關的方法。
2.1.weak表
weak_table_t是一個全局weak 引用的表,使用不定類型對象的地址作為 key,用 weak_entry_t 類型結構體對象作為 value 。其中的 weak_entries 成員
/**
* The global weak references table. Stores object ids as keys,
* and weak_entry_t structs as their values.
*/
struct weak_table_t {
weak_entry_t *weak_entries; //保存了所有指向指定對象的weak指針 weak_entries的對象
size_t num_entries; // weak對象的存儲空間
uintptr_t mask; //參與判斷引用計數輔助量
uintptr_t max_hash_displacement; //hash key 最大偏移值
};
weak全局表中的存儲weak定義的對象的表結構weak_entry_t,weak_entry_t是存儲在弱引用表中的一個內部結構體,它負責維護和存儲指向一個對象的所有弱引用hash表。其定義如下:
typedef objc_object ** weak_referrer_t;
struct weak_entry_t {
DisguisedPtr<objc_object> referent; //范型
union {
struct {
weak_referrer_t *referrers;
uintptr_t out_of_line : 1;
uintptr_t num_refs : PTR_MINUS_1;
uintptr_t mask;
uintptr_t max_hash_displacement;
};
struct {
// out_of_line=0 is LSB of one of these (don't care which)
weak_referrer_t inline_referrers[WEAK_INLINE_COUNT];
};
}
}
在 weak_entry_t 的結構中,DisguisedPtr referent 是對泛型對象的指針做了一個封裝,通過這個泛型類來解決內存泄漏的問題。從注釋中寫 out_of_line 成員為最低有效位,當其為0的時候, weak_referrer_t 成員將擴展為多行靜態 hash table。其實其中的 weak_referrer_t 是二維 objc_object 的別名,通過一個二維指針地址偏移,用下標作為 hash 的 key,做成了一個弱引用散列。
out_of_line:最低有效位,也是標志位。當標志位 0 時,增加引用表指針緯度。
num_refs:引用數值。這里記錄弱引用表中引用有效數字,因為弱引用表使用的是靜態 hash 結構,所以需要使用變量來記錄數目。
mask:計數輔助量。
max_hash_displacement:hash 元素上限閥值。
其實 out_of_line 的值通常情況下是等于零的,所以弱引用表總是一個 objc_objective 指針二維數組。一維 objc_objective 指針可構成一張弱引用散列表,通過第三緯度實現了多張散列表,并且表數量為 WEAK_INLINE_COUNT 。
objc_object是weak_entry_t表中weak弱引用對象的范型對象的結構體結構。
struct objc_object {
private:
isa_t isa;
public:
// ISA() assumes this is NOT a tagged pointer object
Class ISA();
// getIsa() allows this to be a tagged pointer object
Class getIsa();
// initIsa() should be used to init the isa of new objects only.
// If this object already has an isa, use changeIsa() for correctness.
// initInstanceIsa(): objects with no custom RR/AWZ
// initClassIsa(): class objects
// initProtocolIsa(): protocol objects
// initIsa(): other objects
void initIsa(Class cls /*indexed=false*/);
void initClassIsa(Class cls /*indexed=maybe*/);
void initProtocolIsa(Class cls /*indexed=maybe*/);
void initInstanceIsa(Class cls, bool hasCxxDtor);
// changeIsa() should be used to change the isa of existing objects.
// If this is a new object, use initIsa() for performance.
Class changeIsa(Class newCls);
bool hasIndexedIsa();
bool isTaggedPointer();
bool isClass();
// object may have associated objects?
bool hasAssociatedObjects();
void setHasAssociatedObjects();
// object may be weakly referenced?
bool isWeaklyReferenced();
void setWeaklyReferenced_nolock();
// object may have -.cxx_destruct implementation?
bool hasCxxDtor();
// Optimized calls to retain/release methods
id retain();
void release();
id autorelease();
// Implementations of retain/release methods
id rootRetain();
bool rootRelease();
id rootAutorelease();
bool rootTryRetain();
bool rootReleaseShouldDealloc();
uintptr_t rootRetainCount();
// Implementation of dealloc methods
bool rootIsDeallocating();
void clearDeallocating();
void rootDealloc();
private:
void initIsa(Class newCls, bool indexed, bool hasCxxDtor);
// Slow paths for inline control
id rootAutorelease2();
bool overrelease_error();
#if SUPPORT_NONPOINTER_ISA
// Unified retain count manipulation for nonpointer isa
id rootRetain(bool tryRetain, bool handleOverflow);
bool rootRelease(bool performDealloc, bool handleUnderflow);
id rootRetain_overflow(bool tryRetain);
bool rootRelease_underflow(bool performDealloc);
void clearDeallocating_weak();
// Side table retain count overflow for nonpointer isa
void sidetable_lock();
void sidetable_unlock();
void sidetable_moveExtraRC_nolock(size_t extra_rc, bool isDeallocating, bool weaklyReferenced);
bool sidetable_addExtraRC_nolock(size_t delta_rc);
bool sidetable_subExtraRC_nolock(size_t delta_rc);
size_t sidetable_getExtraRC_nolock();
#endif
// Side-table-only retain count
bool sidetable_isDeallocating();
void sidetable_clearDeallocating();
bool sidetable_isWeaklyReferenced();
void sidetable_setWeaklyReferenced_nolock();
id sidetable_retain();
id sidetable_retain_slow(SideTable *table);
bool sidetable_release(bool performDealloc = true);
bool sidetable_release_slow(SideTable *table, bool performDealloc = true);
bool sidetable_tryRetain();
uintptr_t sidetable_retainCount();
#if !NDEBUG
bool sidetable_present();
#endif
};
總之:
1.weak_table_t(weak 全局表):采用hash(哈希表)的方式把所有weak引用的對象,存儲所有引用weak對象
2.weak_entry_t(weak_table_t表中hash表的value值,weak對象體):用于記錄hash表中weak對象
3.objc_object(weak_entry_t對象中的范型對象,用于標記對象weak對象):用于標示weak引用的對象。
詳細講解weak存儲對象結構,對接下來對weak操作使用可以更加清晰的理解weak的使用。
2.2.weak底層實現原理
在runtime源碼中的NSObject.mm文件中找到了關于初始化和管理weak表的方法
- 初始化weak表方法
//初始化weak表
/**
* Initialize a fresh weak pointer to some object location.
* It would be used for code like:
*
* (The nil case)
* __weak id weakPtr;
* (The non-nil case)
* NSObject *o = ...;
* __weak id weakPtr = o;
*
* @param addr Address of __weak ptr.
* @param val Object ptr.
*/
id objc_initWeak(id *addr, id val)
{
*addr = 0;
if (!val) return nil;
return objc_storeWeak(addr, val); // 存儲weak對象
}
- 存儲weak對象的方法
/**
* This function stores a new value into a __weak variable. It would
* be used anywhere a __weak variable is the target of an assignment.
*
* @param location The address of the weak pointer itself
* @param newObj The new object this weak ptr should now point to
*
* @return \e newObj
*/
id
objc_storeWeak(id *location, id newObj)
{
id oldObj;
SideTable *oldTable;
SideTable *newTable;
spinlock_t *lock1;
#if SIDE_TABLE_STRIPE > 1
spinlock_t *lock2;
#endif
// Acquire locks for old and new values.
// Order by lock address to prevent lock ordering problems.
// Retry if the old value changes underneath us.
retry:
oldObj = *location;
oldTable = SideTable::tableForPointer(oldObj);
newTable = SideTable::tableForPointer(newObj);
lock1 = &newTable->slock;
#if SIDE_TABLE_STRIPE > 1
lock2 = &oldTable->slock;
if (lock1 > lock2) {
spinlock_t *temp = lock1;
lock1 = lock2;
lock2 = temp;
}
if (lock1 != lock2) spinlock_lock(lock2);
#endif
spinlock_lock(lock1);
if (*location != oldObj) {
spinlock_unlock(lock1);
#if SIDE_TABLE_STRIPE > 1
if (lock1 != lock2) spinlock_unlock(lock2);
#endif
goto retry;
}
weak_unregister_no_lock(&oldTable->weak_table, oldObj, location);
newObj = weak_register_no_lock(&newTable->weak_table, newObj, location);
// weak_register_no_lock returns nil if weak store should be rejected
// Set is-weakly-referenced bit in refcount table.
if (newObj && !newObj->isTaggedPointer()) {
newObj->setWeaklyReferenced_nolock();
}
// Do not set *location anywhere else. That would introduce a race.
*location = newObj;
spinlock_unlock(lock1);
#if SIDE_TABLE_STRIPE > 1
if (lock1 != lock2) spinlock_unlock(lock2);
#endif
return newObj;
}
- 舊對象解除注冊操作 weak_unregister_no_lock
該方法主要作用是將舊對象在 weak_table 中接觸 weak 指針的對應綁定。根據函數名,稱之為解除注冊操作。從源碼中,可以知道其功能就是從 weak_table 中接觸 weak 指針的綁定。而其中的遍歷查詢,就是針對于 weak_entry 中的多張弱引用散列表。
- 新對象添加注冊操作 weak_register_no_lock
這一步與上一步相反,通過 weak_register_no_lock 函數把心的對象進行注冊操作,完成與對應的弱引用表進行綁定操作。
-
初始化弱引用對象流程一覽
弱引用的初始化,從上文的分析中可以看出,主要的操作部分就在弱引用表的取鍵、查詢散列、創建弱引用表等操作,可以總結出如下的流程圖:
SideTable 這個結構體,是對weak_table_t表的再次封裝操作,避免對weak_table_t直接操作,SideTable使用更加方便。
class SideTable {
private:
static uint8_t table_buf[SIDE_TABLE_STRIPE * SIDE_TABLE_SIZE];
public:
spinlock_t slock;
RefcountMap refcnts;
weak_table_t weak_table;
SideTable() : slock(SPINLOCK_INITIALIZER)
{
memset(&weak_table, 0, sizeof(weak_table));
}
~SideTable()
{
// never delete side_table in case other threads retain during exit
assert(0);
}
static SideTable *tableForPointer(const void *p)
{
# if SIDE_TABLE_STRIPE == 1
return (SideTable *)table_buf;
# else
uintptr_t a = (uintptr_t)p;
int index = ((a >> 4) ^ (a >> 9)) & (SIDE_TABLE_STRIPE - 1);
return (SideTable *)&table_buf[index * SIDE_TABLE_SIZE];
# endif
}
static void init() {
// use placement new instead of static ctor to avoid dtor at exit
for (int i = 0; i < SIDE_TABLE_STRIPE; i++) {
new (&table_buf[i * SIDE_TABLE_SIZE]) SideTable;
}
}
};
總之根據以上對weak進行存儲的過程可以通過下邊的流程圖詳細的描述出來
3.weak釋放為nil過程
weak被釋放為nil,需要對對象整個釋放過程了解,如下是對象釋放的整體流程:
1、調用objc_release
2、因為對象的引用計數為0,所以執行dealloc
3、在dealloc中,調用了_objc_rootDealloc函數
4、在_objc_rootDealloc中,調用了object_dispose函數
5、調用objc_destructInstance
6、最后調用objc_clear_deallocating。
對象準備釋放時,調用clearDeallocating函數。clearDeallocating函數首先根據對象地址獲取所有weak指針地址的數組,然后遍歷這個數組把其中的數據設為nil,最后把這個entry從weak表中刪除,最后清理對象的記錄。
在對象被釋放的流程中,需要對objc_clear_deallocating方法進行深入的分析
void objc_clear_deallocating(id obj)
{
assert(obj);
assert(!UseGC);
if (obj->isTaggedPointer()) return;
obj->clearDeallocating();
}
//執行 clearDeallocating方法
inline void objc_object::clearDeallocating()
{
sidetable_clearDeallocating();
}
// 執行sidetable_clearDeallocating,找到weak表中的value值
void objc_object::sidetable_clearDeallocating()
{
SideTable *table = SideTable::tableForPointer(this);
// clear any weak table items
// clear extra retain count and deallocating bit
// (fixme warn or abort if extra retain count == 0 ?)
spinlock_lock(&table->slock);
RefcountMap::iterator it = table->refcnts.find(this);
if (it != table->refcnts.end()) {
if (it->second & SIDE_TABLE_WEAKLY_REFERENCED) {
weak_clear_no_lock(&table->weak_table, (id)this);
}
table->refcnts.erase(it);
}
spinlock_unlock(&table->slock);
}
對weak置nil的操作最終調用執行weak_clear_no_lock方法用于執行置nil的操作。執行方法如下:
/**
* Called by dealloc; nils out all weak pointers that point to the
* provided object so that they can no longer be used.
*
* @param weak_table
* @param referent The object being deallocated.
*/
void
weak_clear_no_lock(weak_table_t *weak_table, id referent_id)
{
objc_object *referent = (objc_object *)referent_id;
weak_entry_t *entry = weak_entry_for_referent(weak_table, referent);
if (entry == nil) {
/// XXX shouldn't happen, but does with mismatched CF/objc
//printf("XXX no entry for clear deallocating %p\n", referent);
return;
}
// zero out references
weak_referrer_t *referrers;
size_t count;
if (entry->out_of_line) {
referrers = entry->referrers;
count = TABLE_SIZE(entry);
}
else {
referrers = entry->inline_referrers;
count = WEAK_INLINE_COUNT;
}
for (size_t i = 0; i < count; ++i) {
objc_object **referrer = referrers[i];
if (referrer) {
if (*referrer == referent) {
*referrer = nil;
}
else if (*referrer) {
_objc_inform("__weak variable at %p holds %p instead of %p. "
"This is probably incorrect use of "
"objc_storeWeak() and objc_loadWeak(). "
"Break on objc_weak_error to debug.\n",
referrer, (void*)*referrer, (void*)referent);
objc_weak_error();
}
}
}
weak_entry_remove(weak_table, entry);
}
objc_clear_deallocating該函數的動作如下:
1、從weak表中獲取廢棄對象的地址為鍵值的記錄
2、將包含在記錄中的所有附有 weak修飾符變量的地址,賦值為nil
3、將weak表中該記錄刪除
4、從引用計數表中刪除廢棄對象的地址為鍵值的記錄
其實Weak表是一個hash(哈希)表,然后里面的key是指向對象的地址,Value是Weak指針的地址的數組。
總結
weak是Runtime維護了一個hash(哈希)表,用于存儲指向某個對象的所有weak指針。weak表其實是一個hash(哈希)表,Key是所指對象的地址,Value是weak指針的地址(這個地址的值是所指對象指針的地址)數組。