美文网首页
iOS中引用计数相关问题

iOS中引用计数相关问题

作者: 没八阿哥的程序 | 来源:发表于2018-10-30 17:31 被阅读25次

alloc实现

经过一系列调用,最终调用了C函数的calloc方法
此时并没有设置引用计数为1

retain实现

//通过哈希查找,查找到对应的table表
SideTable& table = SideTables()[this];
//通过哈希查找,查找到具体的引用计数值
size_t& refcntStorage = table.refcnts[this];
//加1操作
refcntStorage += SIDE_TABLE_RC_ONE;

SIDE_TABLE_RC_ONE为宏定义,并非为实际的1,因为储存引用计数开始于第二位(0.1.2.3…),这里实际为4,得到的的结果实际就是加1操作

release实现

//通过哈希查找,查找到对应的table表
SideTable& table = SideTables()[this];
//根据当前对象指针,查找到所对应的引用计数表
RefcountMap::iterator it = table.refcnts.find(this);
//减1操作
it->second -= SIDE_TABLE_RC_ONE;

retainCount实现

//通过哈希查找,查找到对应的table表
SideTable& table = SideTables()[this];
size_t& refcnt_result = 1;
//根据当前对象指针,查找到所对应的引用计数表
RefcountMap::iterator it = table.refcnts.find(this);
//向右偏移再加1
refcnt_result+= it->second >> SIDE_TABLE_RC_SHIFT;

dealloc实现

dealloc流程

object_dispose()实现

  1. 开始
  2. objc_destructInstance()
  3. C函数free()
  4. 结束

objc_destructInstance()实现

objc_destructInstance()实现

clearDeallocting()实现

image.png

相关文章

网友评论

      本文标题:iOS中引用计数相关问题

      本文链接:https://www.haomeiwen.com/subject/lsogtqtx.html