Xcode comes with a super easy-to-use diagnostic tool

Welcome to our public account, we will share original articles every week. We are mainly positioned in the field of mobile development, sharing mobile development technologies, including iOS, Android, applet, mobile front-end, React Native, weex, etc.

Knowledge Collection GitHub

"Damn it, how do I check this question? I've tried dozens of times, but I can't reproduce it?", "I'll go, this question makes me unbelievable"... I often hear this kind of voice, so today I'm going to make it easy for everyone Introduce Xcodeseveral super easy-to-use diagnostic tools that come with them to improve your work efficiency. (This article will not introduce detailed usage methods, please Google or Baidu by yourself.)

Be kind to compilation warnings

XcodeWe have prepared a very rich compilation warning for us. Although these warnings will not lead to compilation, there must be no hate for no reason behind it. Treat Warnings as ErrorsIt's also a good idea to treat all warnings correctly and avoid some elusive behavior at runtime . If you are absolutely confident about some warnings, you can eliminate them by Build Settingsmodifying the Warningsoptions in , or by adding diagnostic ignoreto . With Xcodethe continuous update of , you should also Editor-Validate Settingschange the project configuration options to optimal through the project file's .

Don't Ignore Analyze

XcodeStatic detection tools have been provided for a long time for us to detect a range of hard-to-find problems, such as memory leaks, logic errors, unused variables, and libraries not included. For many projects, a full run Analyzewill take a long time, and perhaps because of this, this tool is being ignored by more and more developers. You can perform Analyze in real-time during the usual development and debugging process by modifying Build Settingsit .Analyze During 'Build'

runtime diagnostics

In addition to compilation warnings Analyze, Apple also provides us with more dynamic debugging tools. They are scattered Xcodein every corner of , and are closely related to our daily development.

Debug Gauges

When you Appstart debugging, Xcodethe navigatorview will automatically switch to debug navigator. Here, several key metrics are displayed by default to give you an overview of Appthe operational status. Through them, you can get a general understanding of the operation of each thread, memory application, power usage, disk read and write, network operations, etc. If you care about more detailed diagnostic information, you can easily enter Instrumentsto make the corresponding diagnosis.

###Process Debug

XcodeProvides us with four kinds of process debugging:

  • View Process by Thread: The call stack of the thread, this is the default and the most commonly used. Add a breakpoint to view all current thread calls.

  • View Process by Queue: View Process by ThreadSimilar to , the current queue name will replace the Threadname, and the color should be richer.

  • View UI Hierarchy: You can switch to God's perspective to view the view hierarchy and view properties.

  • View Memory Graph Hierarchy: It can easily detect memory leaks such as circular references, which is very practical.

Enable Diagnostics

In addition Xcodeto providing us with Guard Malloc, Zombie Objectsetc. in the early days, Sanitizersand Main Thread Checkerfurther improve the detection tools in the development process.

  • Address Sanitizer

For the MRCenvironment , OCit will be triggered to operate on an object after it is released crash; after entering the ARCera , we care less and less about this issue. But when you use Core Foundation, or C\C++mix , this problem can behave unexpectedly. When a memory area is freed, there is no problem accessing it until the area is reallocated, which brings a lot of randomness. In addition, if you accidentally cross the bounds when using pointers to access memory, the consequences are unpredictable. For these problems, Xcodeis provided Address Sanitizerto test more robustly.

  • Thread Sanitizer

In the past, Data Raceit was very difficult to locate and troubleshoot problems, because the problems it brought were often unpredictable. XcodeBy recording information such as the time of each memory access, the detection is performed each time the memory is accessed. You can selectively enable this tool in the daily development and debugging process, but in unit testing, you should consider enabling it. If your use case coverage is high, it may be able to find most of the multi-threaded data competition for you. question.

  • Undefined Behavior Santizer

Undefined text detection, such as dividing by 0, loading memory to its pointer, non-associating a null pointer, etc. It may crashor crash, depending on the specific situation and the compiler, but we should avoid this kind of thing from happening.

  • Main Thread Checker

Detection is used in child threads AppKit, UIKitand others API, this detection tool is enabled by default, and does not need to be recompiled after it is enabled. The principle is to automatically replace the API that can only be used in the main thread after the app is started, and throw an exception when calling such an API.

It is worth mentioning that since the implementation principle of Address Sanitizer, orGuard Malloc directly or indirectly affects the memory management of runtime objects, if these diagnostic tools are enabled, one will fail.Zombie ObjectsDebug GaugesMemory

Summarize

Solving warnings and diagnoses as soon as possible Appwill greatly improve Appthe stability after going online, and it can also improve our work efficiency, so what are you waiting for, act quickly.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326690850&siteId=291194637