# 02 iOS-常见崩溃分析

## 1: 字典插入nil

* 通过Runtime替换,为系统方法添加容错
  * 同时添加日志,上报记录错误
  * 保证程序运行稳定的情况下,暴露错误

## 2: 越界

* 处理方式同上

## 3. unrecognized selector

* 可以再消息传递流程内进行防护

## 4. TableView & CollectionView cell代理方法存在返回 nil 的情况

* 别偷懒

## 5. EXC\_BAD\_ACCESS

* 开启僵尸对象调试选项进行调试

## 6. SIGPIPE 异常

> 经实验在多个ios版本下都不再触发SIGPIPE，完美解决问题。

* 对一个端已经关闭的socket调用两次write，第二次write将会产生SIGPIPE信号，该信号默认结束进程

在pch中:

```
// 仅在 IOS 系统上支持 SO_NOSIGPIPE
#if defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL)
    // We do not want SIGPIPE if writing to socket.
    const int value = 1;
    setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(int));
#endif
```

## 7. 子线程UI操作崩溃

* 放主线程

## 8. SIGABRT 异常

> 这是一个让程序终止的标识，会在断言、app内部、操作系统用终止方法抛出。通常发生在异步执行系统方法的时候。如CoreData、NSUserDefaults等，还有一些其他的系统多线程操作。 这并不一定意味着是系统代码存在bug，代码仅仅是成了无效状态，或者异常状态。

## 9. dispatch\_group崩溃

* enter 数量和 leave 数量要匹配

## 10. 对象释放的线程要和它处理事情的线程一致

## 参考

> <https://www.jianshu.com/p/c7efbc283480> <https://www.jianshu.com/p/c4bd2960b3fa>


---

# 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/issue/ios-chang-jian-beng-kui-fen-xi.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.
