unrecognized selector sent to instance - iphone

I added an index search to a core data backed UITableView. the search works fine, however after navigating back to the tableView I get this error:
-[NSSQLRow controllerDidChangeContent:]: unrecognized selector sent to instance 0x815edf0
I can post more code if this is too little information to go on.
thanks for any help

In Xcode (3), enable:
Run > Stop on Objective-C Exceptions
run your program in Debug.
Ultimately what is happening is an (objc) object is requested to perform a message which it does not respond to (i.e. is not implemented).
Typically, this happens as a programmer's mistake (at least, for me), such as an argument passed as another type, which slips through a cast, id, or objc_object container (e.g. any collection class - NSArray, NSSet, NSDictionary).
Sometimes this happens if you forget to implement the instance method.
Sometimes this happens if you are testing against an earlier release of the software, which did not implement the instance method (i.e. it was added in a following release).

Related

Threading within ASIHTTPRequest

I have a singleton wrapper class called "Folio APIWrapperv2" that is responsible for handling all of my API data calls within my iPhone app.
This class implements ASIHTTPRequest in order to fetch JSON from my server. When an ASIHTTPRequest has finished, it calls requestFinished:. In this method, I have additional processing of the data that can take a couple of seconds to complete. It's not major, but it slows down the app slightly, as it's a blocking call. To fix this, I create a new thread using GCD. However, when this thread runs, I get the following error:
[FolioAPIWrapperv2 respondsToSelector:]: message sent to deallocated instance 0x245050
This function is being called in ASIHTTPRequest's "handleBytesAvailable" method at the line:
if ([[self delegate] respondsToSelector:[self didReceiveDataSelector]]) {
I'm not sure what's going on here. FolioAPIWrapperv2 is a singleton class, so it should never be deallocated. I've tried searching online to see what other issues people have had with threading and couldn't find a suitable solution. Does anyone have an ideas?
Are you setting the delegate to be an instance of the class or the class itself? The delegate must be an instance of a class.
Otherwise, if you're getting the output:
[FolioAPIWrapperv2 respondsToSelector:]: message sent to deallocated instance 0x245050
Then it seems likely that your class that should never be deallocated is getting deallocated.
Either way, at least one way to approach this is to add some debugging, eg, when you create the request:
NSLog("set delegate to %p", request.delegate);
and similar logging for self in the init/dealloc of your singleton.

message sent to deallocated instance

I'm having a table view of data. While scrolling my table 2 or 3 times so fast, its getting crashed. My gdb is telling like
"message sent to deallocated instance...."
Anyone know how to solve this?
yes, your reference counting has errors.
to find the object, enable NSZombies. enabling NSZombies will not free your objects -- but will generate runtime errors when you attempt to message an instance which would (under normal operation) have been deallocated. from there, you can learn more about the object (e.g. its type).
Instruments also has a NSZombie mode. it is very useful to point out an object/zombie's lifetime.
for more details, see:
http://www.cocoadev.com/index.pl?NSZombieEnabled
the link also details how you can configure your executable to run with zombies enabled. this is found in the section 'Use in Xcode'.

What are the most common/uncommon error messages one encounters in Objective-C and what causes them?

I've been trying to maintain a list of all the error messages I ever encounter while developing and their common causes and fixes. What are the common (and the not so common) error messages you encountered and how did you fix them?
EXC_BAD_ACCESS when referencing a pointer that points to an object that has been dealloc'ed
In general I find objective-c exception and error messages tO be self-explanatory. I think you would be better served by learning to use the debugger with breakpoints on objective-c exceptions to locate the specific line causing a crash than by trying to compile a list of recipies for resolving common errors.
That said the one non-obvious error I see as a frequent point of confusion is "unrecognized selector foo: sent to BarClass". While the reason why that is an error should be clear answering how that happened can be harder and I see two common causes:
An app is attempting to call a subclass' method on an instance of a super class. Often a result of developers failing to change the class of objects in IB.
An object has been over-released or otherwise referenced after being deallocated. If a new instance of some other class now occupies that memory address the app with hit an "unrecognized selector" exception rather than a "BAD_ACCESS".

How to trace out all messages sent to a particular class or instance in Objective-C?

I would like to trace out all messages sent by the Objective-C runtime to a particular class, say UITableView (or a particular instance) so I can better understand the inner workings of some classes. Is there a way to do this?
Another use case is to trace out all delegate methods that are being called (say UITableViewDelegate methods) without having to declare them and put a trace method in each of them.
This may be a little heavy-handed, but try setting the NSObjCMessageLoggingEnabled environment variable to YES. That will show all the messages sent to every object, but you can easily filter it down to the messages sent to a particular class.
Some good tips here: http://www.dribin.org/dave/blog/archives/2006/04/22/tracing_objc/
Disclaimer: I'm not sure if the environment variable works for iPhone.

How do I restore a NSUndoManager's contents in a CoreData NSManagedObjectContext?

I'd like to use NSUndoManager in an iPhone application on CoreData (NSManagedObject) objects such that I can save (and later restore) the state of the NSUndoManager if the application exits prematurely (say, due to a phone call coming in). I.e. as opposed to automatically discarding or saving the changes accumulated in the NSUndoManager, I would like to restore them so that the user has the option to explicitly discard or save them when they restart the app.
Has anyone had any experience with this? Can anyone recommend this (or an alternative) approach to managing pending changes in a NSManagedObjectContext when the application is interrupted?
The NSUndoManager does not actually store state, it stores a stack of actions that will restore the state. For example, if you have an object XXX and it has a property name which is a string and you change that name from "Steve" to "Joe", what the NSUndoManager stores is a target, selector and object. The target would be the instance of XXX, the selector would be #selector(setName:) and the object would be #"Steve".
By storing that information, if the undo stack is popped it will call -setName: on the instance of object XXX with the value of #"Steve" and thus restoring its state. There is some additional work done around KVO, etc. but that is the basics.
At first I theorized that you could write out the NSManagedObjectID, the selector (using NSStringFromSelector) and the object to disk and restore them by calling -registerUndoWithTarget: selector: object:. However upon further review of the documentation, there is no way to access the stack to be able to iterate over it.
Note that one possible work-around exists by using separate NSManagedObjectContexts such that some are saved on shutdown whereas others have their changes rolled back. It's not perfect, but I found a suitable solution to my problem with this alternative.