I keep getting the following error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString setLinkID:]: unrecognized selector sent to instance 0x6bf37e0'
I’m using NSXMLParser to parse an XML document, and when I run across the tag “link”, I am creating a custom JLink object and passing the parser delegate along to that object. This method was working fine and then I must have done something because now it is suddenly producing the error above every time I run the project.
I’ve been pulling my hair out, but I think that the JLink object is being released and therefore when the method setLinkID: is called, the program crashes. Has anybody else had this problem or know what may be the problem? My code is below:
Method that is causing the error (most of the) time but sometimes it changes:
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqual:@"linkID"]) {
currentString = [[NSMutableString alloc] init];
[self setLinkID:currentString];
} else if ([elementName isEqual:@"userID"]) {
currentString = [[NSMutableString alloc] init];
[self setUserID:currentString];
} else if ([elementName isEqual:@"url"]) {
currentString = [[NSMutableString alloc] init];
[self setLink:currentString];
} else if ([elementName isEqual:@"displayText"]) {
currentString = [[NSMutableString alloc] init];
[self setText:currentString];
}
}
And the method that is creating JLink and making it the parser’s delegate:
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqual:@"userID"]) {
currentString = [[NSMutableString alloc] init];
[self setUserID:currentString];
[links removeAllObjects];
} //A bunch of other checks
else if ([elementName isEqual:@"link"]) {
JLinks* newLink = [[JLinks alloc] init];
//Setup the parent so that we can regain control of the element
[newLink setParentParserDelegate:self];
[parser setDelegate:newLink];
[[self links] addObject:newLink];
}
}
I’m using ARC. Thanks
OK, the error says
Note that you’re somehow sending the
setLinkID:message to an instance of__NSCFString(basically a private subclass ofNSString).In your code, you are only sending
setLinkID:to self. So, somehow your parser delegate has been deallocated and, in its place, an instance of NSString has been allocated.Even if you’re using ARC, you still have to understand object ownership. In this case, the NSXMLParser does not retain a reference to its delegate, because that would very likely create a retain cycle. If you look in the headers or the documentation you will see that the property is defined as a weak reference.
What I think is happening is that you set up the parser, you have no other strong reference to the main parser delegate, so shortly after parsing begins, it is deallocated. You need to go through your code and make sure you have a strong reference to that main parser delegate.
You will have problems if you do this:
You should structure your code like this:
If you use the pattern of swapping in specialized delegates, the topLevelDelegate needs to maintain a strong reference to the child delegates. If you have a strong reference from the child to the parent, it won’t matter, because the child itself will only be reachable from the parser itself, which doesn’t retain its delegate. You will probably need a reference both ways: strong from parent to child; weak from child to parent. (In general, this pattern will prevent retain cycles.)
Oh, to answer your title question: Yes, it’s possible for ARC to release memory you are using, if it is only reachable by weak references to it. But if you’re using it, you should have at least one strong reference to it.