Receiving Error With NSXMLParser: [Parser conformsToProtocol:]: Message sent to deallocated instance 0xcd6be20
I am receiving this error from the NSXMLParser when I pop the NSXMLParser view contr开发者_高级运维oller, and then go back to it later on.
2010-12-31 21:49:32.306 App Name[12716:207] *** -[Parser conformsToProtocol:]: message sent to deallocated instance 0xcd6be20
:
Here is my code: https://gist.github.com/761366
It's impossible to say from the snippet you provided exactly what's going on, but this error generally means you've overreleased the object being messaged. My reading of your description and your code is that the parser is longer lived than the object you're setting as its delegate here:
- (void)parseXMLFileAtURL:(NSString *)URL
{
stories = [[NSMutableArray alloc] init];
NSURL *xmlURL = [NSURL URLWithString:URL];
parser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
}
It's impossible to tell from the snippet you posted what the scope of the variable parser is. I'm suspecting it's a global variable or otherwise longer-lived than the delegate. I suspect this, because I see that you're setting the 'self object' to be the delegate, but never unsetting it, and the fact that the erroneous message is conformsToProtocol: hints that this might be the issue, because NSXMLParser will undoubtedly call that before attempting to call any delegate methods. When this object is going away, it needs to relinquish it's role as the delegate of the NSXMLParser instance by calling:
[parser setDelegate: nil];
If the parser is, in fact, global or otherwise shared, then you should also be careful about recreating it on every call to parseXMLFileAtURL: without releasing prior values. If it's an iVar on the object itself, you still need to be sure and clear the delegate and release and clear the iVar when your object goes away (i.e. in dealloc).
But again, it's impossible to be sure based on the snippet you've posted.
This is the message you'll get when you're releasing an object too many times. I see that you've got a custom autorelease pool set up, which could be the culprit, but you've probably got that since you're running multiple threads. It looks like your tableViewController has an instance variable named parser
, if you have declared that as a property, you might want to check that it is set to retain. Without seeing the full header declaration and implementation, it's tough to say exactly where the release is happening.
The way I usually handle these errors is to enable NSZombie tracking in Xcode. This will give you a stacktrace when a deallocated object is referenced, and allow you to look at your application state in the xcode debugger.
Here's a link with instructions for configuring Xcode to enable NSZombies. http://www.cocoadev.com/index.pl?NSZombieEnabled
精彩评论