Objective-C 对象释放方法调用过程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Objective-C 对象释放方法调用过程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3106字,纯文字阅读大概需要5分钟。
内容图文
![Objective-C 对象释放方法调用过程](/upload/InfoBanner/zyjiaocheng/1037/933f178c18904b9583e815b5a28072d2.jpg)
1.objc_object::rootDealloc
inline void
objc_object::rootDealloc()
{
if (isTaggedPointer()) return; // fixme necessary?
if (fastpath(isa.nonpointer && // 开启指针优化
!isa.weakly_referenced && // 无弱引用指向
!isa.has_assoc && // 无关联对象
!isa.has_cxx_dtor && // 无 C++ 析构函数
!isa.has_sidetable_rc)) // 未使用 SideTable
{
assert(!sidetable_present());
free(this);
}
else {
object_dispose((id)this);
}
}
2.object_dispose
/***********************************************************************
* object_dispose
* fixme
* Locking: none
**********************************************************************/
id
object_dispose(id obj)
{
if (!obj) return nil;
objc_destructInstance(obj);
free(obj);
return nil;
}
3.objc_destructInstance
/***********************************************************************
* objc_destructInstance
* Destroys an instance without freeing memory.
* Calls C++ destructors.
* Calls ARC ivar cleanup.
* Removes associative references.
* Returns `obj`. Does nothing if `obj` is nil.
**********************************************************************/
void *objc_destructInstance(id obj)
{
if (obj) {
// Read all of the flags at once for performance.
bool cxx = obj->hasCxxDtor(); // 类及父类是否有自己的析构函数
bool assoc = obj->hasAssociatedObjects(); // 当前对象是否有关联对象
// This order is important.
if (cxx) object_cxxDestruct(obj);
if (assoc) _object_remove_assocations(obj);
obj->clearDeallocating();
}
return obj;
}
4.objc_object::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());
}
5.objc_object::clearDeallocating_slow
// Slow path of clearDeallocating()
// for objects with nonpointer isa
// that were ever weakly referenced
// or whose retain count ever overflowed to the side table.
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) {
table.refcnts.erase(this); // 清空 DenseMap 中的引用计数数据
}
table.unlock();
}
// 未使用指针优化调用
void
objc_object::sidetable_clearDeallocating()
{
SideTable& table = SideTables()[this];
// clear any weak table items
// clear extra retain count and deallocating bit
// (fixme warn or abort if extra retain count == 0 ?)
table.lock();
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);
}
table.unlock();
}
?
1人点赞
?
ios
?
作者:MangK
链接:https://www.jianshu.com/p/0b77593e10aa
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
内容总结
以上是互联网集市为您收集整理的Objective-C 对象释放方法调用过程全部内容,希望文章能够帮你解决Objective-C 对象释放方法调用过程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。