Skip to content

Commit 05038d6

Browse files
author
Draveness
committed
Add retain count section to rr article
1 parent 5a04f78 commit 05038d6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

objc/黑箱中的 retain 和 release.md

+27
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,33 @@ bool objc_object::rootRelease(bool performDealloc, bool handleUnderflow) {
328328
329329
不过为了确保消息只会发送一次,我们使用 `deallocating` 标记位。
330330
331+
## 获取自动引用计数
332+
333+
在文章的最结尾,笔者想要介绍一下 `retainCount` 的值是怎么计算的,我们直接来看 `retainCount` 方法的实现:
334+
335+
```objectivec
336+
- (NSUInteger)retainCount {
337+
return ((id)self)->rootRetainCount();
338+
}
339+
340+
inline uintptr_t objc_object::rootRetainCount() {
341+
isa_t bits = LoadExclusive(&isa.bits);
342+
uintptr_t rc = 1 + bits.extra_rc;
343+
if (bits.has_sidetable_rc) {
344+
rc += sidetable_getExtraRC_nolock();
345+
}
346+
return rc;
347+
}
348+
```
349+
350+
根据方法的实现,retainCount 有三部分组成:
351+
352+
+ 1
353+
+ `extra_rc` 中存储的值
354+
+ `sidetable_getExtraRC_nolock` 返回的值
355+
356+
这也就证明了我们之前得到的结论。
357+
331358
## 小结
332359

333360
我们在这篇文章中已经介绍了 `retain``release` 这一对用于内存管理的方法是如何实现的,这里总结一下文章一下比较重要的问题。

0 commit comments

Comments
 (0)