# 06.dealloc

Dealloc 的实现机制是内容管理部分的重点，把这个知识点弄明白，对于全方位的理解内存管理的只是很有必要。

## 1. Dealloc 调用流程

1. 首先调用 `_objc_rootDealloc()`
2. 接下来调用 `rootDealloc()`
3. 这时候会判断是否可以被释放，判断的依据主要有 5 个，判断是否有以上五种情况
   * NONPointer\_ISA
   * weakly\_reference
   * has\_assoc
   * has\_cxx\_dtor
   * has\_sidetable\_rc
4. 如果有以上五中任意一种，将会调用 `object_dispose()` 方法，做下一步的处理。
5. 如果没有之前五种情况的任意一种，则可以执行释放操作，`C` 函数的 `free()`。
6. 执行完毕。

## 2. object\_dispose() 调用流程

1. 直接调用 `objc_destructInstance()`。
2. 之后调用 `C` 函数的 `free()`。

## 3. objc\_destructInstance() 调用流程

1. 先判断 `hasCxxDtor`，如果有 `C++` 的相关内容，要调用 `object_cxxDestruct()` ，销毁 `C++` 相关的内容。
2. 再判断 `hasAssocitatedObjects`，如果有的话，要调用 `object_remove_associations()`， 销毁关联对象的一系列操作。
3. 然后调用 `clearDeallocating()`。
4. 执行完毕。

## 4. clearDeallocating() 调用流程

1. 先执行 `sideTable_clearDellocating()`。
2. 再执行 `weak_clear_no_lock`，在这一步骤中，会将指向该对象的弱引用指针置为 `nil`。
3. 接下来执行 `table.refcnts.eraser()`，从引用计数表中擦除该对象的引用计数。
4. 至此为止，`Dealloc` 的执行流程结束。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ryukiedev.gitbook.io/wiki/ios/nei-cun-guan-li/06.dealloc.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
