How to find out the location of unrecognized selector exception in Objective-C? - objective-c

How do I go about finding out where in my code caused the following exception?
2012-08-15 09:24:27.414 TestProject[82870:17303] -[TestObj doIt]: unrecognized selector sent to instance 0x1106f320

Best way to do it: Add a breakpoint to capture all exceptions, that will give you the line of code where you are getting the exception. From the console, you will get the same message you are posting on your question, so, use the pointer address to print the object that is getting the exception. If the object is garbage(the debugger wont print it), that means you are overreleasing an object. If you have zombies enabled, you will find a prefix NSZombie__ on your class name. That also means overrelease. If you get a different class than the one you are expecting, you are switching the objects at some point and sending a message to the wrong object.

Go to the breakpoints navigator (on the left)
at the bottom you have a +,
add an exception breakpoint on all exceptions

set a breakpoint for thrown exceptions. by default, it will pause when an exception is thrown -- there you will see the backtrace and values.
if it's completely random (e.g. not reproducible), then you may have best luck running Instruments with zombies enabled.

Related

How to understand which unrecognized selector sent to instance

How do i figure out which selector is this , from this error message ?
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0x178059b30'
how do I make xcode give me normal error messages, ones that will tell me where the problem is exactly?
That is a normal error message. The system can't tell you more than this.
But with the help of the debugger (exception breakpoints enabled), and a good understanding of Objective-C you should be able to diagnose the problem.
Here is what we know from the exception message:
The unrecognized selector was length. The most common class that has this method is NSString.
The object that received this method was a NSArray
After the exception breakpoint was triggered type po 0x178059b30 into the debugger to inspect the array. This content of the array can give you hints to figure out how the problem was caused initially.
From the debugger position in your code you can see what triggered the bug.
You say your code failed at [_label setText:name].
This method expects a NSString, during assignment a couple of NSString specific methods are called, length is one of these methods. Which confirms point one of our guess.
There are two possibilities:
name is actually a NSArray because you wrote code that assigned the wrong object. This often happens if you don't check the results you get from a dictionary or an array.
You have a memory problem and name was deallocated prematurely and an array is now where the NSString should be.
To rule out the second option a run of the Analyzer is often enough. It will point out most memory problems.
Another way to decide which of the two is more likely you use the output of po 0x178059b30. If the array holds objects that are related to the expected value of name (e.g. it actually contains name) there is a high chance that you did assign the wrong object. In this case look where you set name, and put a check around it.
If you use a property you can use something like this to figure out where you assign the wrong object:
- (void)setName:(NSString *)name
_name = [name copy];
if (![name isKindOfClass:[NSString class]]) {
NSAssert(NO, #"Wrong class %#", name);
}
}
A breakpoint will be triggered when the assigned object is not a NSString instance, and you can inspect the call stack to see where you did something wrong.
Make sure to remove that code after you are done debugging. You MUST NOT use such checks to prevent bugs in your code.

How to use Instruments to determine part of the code that crashes

I am trying to debug a crash on my app
all i get is
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
I know its a problem with a fetch from CoreData that is returning nil.
Now my objective is to see where the crash is it coming from exactly.
I know there is a way to check with Instruments the exact line of code that causes my code to crash.
Could anyone point me in the right direction on which instrument would give me that information and some debugging tips when trying to find the line in Analyzer?
As per your error log, in my understanding you trying to insert nil value in NSDictionary/NSMutableDictionary. You can not pass nil as value or key into dictionary otherwise your code will crash.
My suggestion you debug you code using breakpoint, trace you execution stack i believe you will get your exact solution where your nil value inserting in dictionary.
If you want to find exact line of crash you can take help of debugger command. Enter bt (back trace command) in debugger window, it will give also same executed crash stack that give you instrument analyzer. Go through following links
https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_5_0.html
http://www.cimgf.com/2012/12/13/xcode-lldb-tutorial/

Objective-C parsing JSON NSNULL error, location not shown

I am parsing quite a large JSON model, and I have a LOT of objects set up to handle all the data. I recently added more data to the JSON model, to test it out, and I am getting this error
-[NSNull count]: unrecognized selector sent to instance 0xaed678
However, I need to know EXACTLY which function is crashing in order to fix it, but for some reason, xCode does not tell you where the function crashes which seems very odd, as that is very important information if you want to rectify the bug. Is there anyway for me to find out exactly which method or function caused the application to crash?
Thanks in advance
Set a breakpoint on unrecognized selector:
Creating breakpoint in Xcode for unrecognized selector
Then trace back in the code.

How to use Xcode output to determine source of crash?

I have the following output from my app at the moment:
2012-09-14 11:55:32.558 projectname[2172:707] -[__NSCFBoolean isEqualToString:]:
unrecognized selector sent to instance 0x3ec4ba18
2012-09-14 11:55:32.570 projectname[2172:707] *** Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason: '-[__NSCFBoolean isEqualToString:]:
unrecognized selector sent to instance 0x3ec4ba18'
*** First throw call stack:
(0x3263788f 0x3468d259 0x3263aa9b 0x32639915 0x32594650 0x41e47 0x417d3 0x46af7
0x320beefb 0x320bdfd9 0x320bd763 0x32061f15 0x325961fb 0x342faaa5 0x342fa6bd
0x342fe843 0x342fe57f 0x342f64b9 0x3260bb1b 0x32609d57 0x3260a0b1 0x3258d4a5
0x3258d36d 0x316e4439 0x3208ccd5 0x17e77 0x15ca4)
terminate called throwing an exception
I can see that the problem is that I'm trying to compare a BOOL to an NSString, the comparison is on data from a web service and it's always been BOOL before now. That's besides the point in this case anyway.
What information is in that log that can actually help me find the line of code that's causing the problem? I can see which instances (e.g. 0x3ec4ba18) are causing it but the log doesn't even tell me what type they are, let alone a line number.
Add an exception breakpoint by going to the exceptions pane (in the left sidebar), clicking the + and selection "Exception breakpoint". Then when you run the debugger will pause where the exception is actually thrown rather then when it is caught (or rather uncaught) at the top level.
There are many tutorials on this, Raywinderlich has one of good tutorial for determining crash from the console log..
Here you can get them..
My App Crashed, Now What? – Part 1
My App Crashed, Now What? – Part 2
After following these, come to know the specific reason of crash then google for this, you can resolve it easily.

Objective-c error "-[CFString retain]: message sent to deallocated instance 0x4593540"

Im using phonegap and currently I have a webapp with php and javascript / html running inside of an iframe. It worked for a while but now it crashes with this error when I load the page
-[CFString retain]: message sent to deallocated instance 0x4593540
any ideas? thanks
You have a retain count problem. From another Stack Overflow answer:
First, go back and reread the memory
management rules just to make sure
you are not missing anything obvious
Next, turn on NSZombieEnabled (in your
executable settings, Arguments panel,
add an environment variable
NSZombieEnabled set to YES).
Double releasing when it shouldn't be happening
Here's a helpful post about NSZombieEnabled
The static analyzer might find the issue (Build menu >> Build and Analyze).
Also, enable "Stop on Objective-C Exceptions" under the Run menu (activate breakpoints first). Then run the app and it will stop when it hits the exception. Then you can use the debugger to find the line where the error occurred.