IOS development crash defense

Time:2022-1-2

1. Crash may occur in IOS

  • Unrecognized selector sent to instance
  • Unrecognized selector sent to class
  • KVO Crash
  • KVC Crash
  • NSNotification Crash
  • NSTimer Crash
  • Container crash (crash caused by collection class operations, such as array out of bounds, inserting nil, etc.)
  • Nsstring crash (crash caused by string operation)
  • Bad access crash (wild pointer)
  • Threading crash (non main thread brushing UI)
  • NSNull Crash

2. Common crash

Objective-CLanguage is a dynamic language that we can useObjective-CLanguageRuntimeRuntime mechanism, which is required forHookAdd new classCategory, in each category+(void)load; Passed inMethod SwizzlingIntercept the system method that is easy to cause crash, and combine the original system method with the added protection methodSelector (method selector)AndImp (function implementation pointer)Swap. Then add protection operation in the replacement method, so as to avoid and repair the crash.

3.KVO Crash

KVO addition times and removal times do not match:
Unregistered observers were removed, resulting in a crash.
Repeatedly remove more times than add, resulting in crash.
Adding multiple times will not crash, but changes will be observed multiple times at the same time.
The observed is released in advance, and the observed still registers KVO during dealloc, resulting in collapse.
For example, if the observed is a local variable (IOS 10 and before will crash).
An observer was added, but the observevalueforkeypath: ofobject: Change: Context: method was not implemented, resulting in a crash.
Keypath = = nil when adding or removing, resulting in a crash.

Article reference:https://www.jianshu.com/p/e3713d309283

4.KVC Crash

There are several reasons for the collapse of KVC in daily use:

Key is not an attribute of the object, causing a crash.
The keypath is incorrect, causing a crash.
The key is nil, causing a crash.
Value is nil, which sets a value for a non object, causing a crash.

Recommended Today

Vue2 technology finishing 3 – Advanced chapter – update completed

3. Advanced chapter preface Links to basic chapters:https://www.cnblogs.com/xiegongzi/p/15782921.html Link to component development:https://www.cnblogs.com/xiegongzi/p/15823605.html 3.1. Custom events of components 3.1.1. Binding custom events There are two implementation methods here: one is to use v-on with vuecomponent$ Emit implementation [PS: this method is a little similar to passing from child to parent]; The other is to use ref […]