Optimize the display of stack trace crashes from Tang Qiao

Source: Internet
Author: User

Intmain (intargc, char * argv []) {

@ Autoreleasepool {

// The exception stops in the following line, which has no effect.

Returnuiapplicationmain (argc, argv, nil, nsstringfromclass ([myclassclass]);}

}

Some error messages are reported on the xcode console interface, as shown in:

Based on the text prompts in the console, I guess the NULL pointer nil has occurred. However, you do not know the specific row of the error. Although the bug was found, the debugging process did take some time. Considering that this Stace trace information should be helpful to me, I checked how to make the original heap call stack information more readable. So I found two good solutions on stackoverflow and shared them here.

Method 1

First, define a method in your appdelegate to handle exceptions:

Void uncaughtexceptionhandler (nsexception * exception ){
Nslog (@ "crash: % @", exception );
Nslog (@ "stack trace: % @", [Exception callstacksymbols]);
// Internal Error Reporting
}

Then, when the application starts, set this method as your own custom exception callback:

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary
*) Launchoptions
{
Nssetuncaughtexceptionhandler (& uncaughtexceptionhandler );
// Normal launch stuff
}

After the preceding exception is completed, after the callback is defined, the log information becomes as follows, and the error line is clear at a glance, according to the following readable Stack
Trace, I can find the exception caused by row 378th OF THE questionparser class, and then jump to the error line to analyze the cause, it is easy to fix the bug.

Ape [2711: 11303]
Crash: ***-[_ nsplaceholderdictionary initwithobjects: forkeys: Count:]: Attempt to insert nil object from objects [2]
Ape [2711: 11303] stack trace :(
0 corefoundation 0x0209402e _ predictionpreprocess + 206
1 libobjc. A. dylib 0x01a71e7e objc_exception_throw + 44
2 corefoundation 0x0205aa95-[_ nsplaceholderdictionary initwithobjects: forkeys: Count:] + 165
3 corefoundation 0x020874e9 + [nsdictionary dictionarywithobjects: forkeys: Count:] + 73
4 ape 0x00096a0a + [questionparser parsetodictionary:] + 378
5 ape 0x00096434-[questionstore putquestion:] + 308
6 ape 0x00089ddf-[questionviewcontroller requestfinished:] + 303
7 ape 0x000869dd-[networkagent requestfinished:] + 653
8 ape 0x00085d33 _ 27-[networkagent addrequest:] _ block_invoke_0 + 131
9 libdispatch. dylib 0x01cf153f _ dispatch_call_block_and_release + 15
10 libdispatch. dylib 0x01d03014 _ dispatch_client_callout + 14
11 libdispatch. dylib 0x01cf2fd6 _ dispatch_after_timer_callback + 28
12 libdispatch. dylib 0x01d03014 _ dispatch_client_callout + 14
13 libdispatch. dylib 0x01cfa8b7 _ dispatch_source_latch_and_call + 219
14 libdispatch. dylib 0x01cf6405 _ dispatch_source_invoke + 322
15 libdispatch. dylib 0x01cf3768_dispatch_main_queue_callback_4cf + 187
16 corefoundation 0x0203aaf5 _ cfrunlooprun + 1925
17 corefoundation 0x02039f44 cfrunlooprunspecific + 276
18 corefoundation 0x02039e1b cfrunloopruninmode + 123
19 graphicsservices 0x0282b7e3 gseventrunmodal + 88
20 graphicsservices 0x0282b668 gseventrun + 104
21 uikit 0x00be265c uiapplicationmain + 1211
22 ape 0x00016c5d main + 141
23 ape 0x00002b05 start + 53
24 ??? 0x00000001 0x0 + 1
)

Method 2

Method 2 is simpler than method 1. In xcode, press cmd + 6 to the breakpoint tab, and click the + sign in the lower left corner to add an exception breakpoint, as shown in. In this way, when an exception occurs, it will automatically stop at the exception and will not be thrown to uiapplicationmain. Taking my bug programs as an example, the code will be automatically broken in line 378th of the questionparser class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.