Re: NSXMLParser frees itself on error?
Re: NSXMLParser frees itself on error?
- Subject: Re: NSXMLParser frees itself on error?
- From: Keary Suska <email@hidden>
- Date: Fri, 1 May 2009 08:35:31 -0600
On May 1, 2009, at 12:59 AM, Mike Manzano wrote:
I have an NSXMLParser doing parsing the contents of a URL. It is
allocated like this:
_showsParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
Given a good URL to a parsable XML document, its
parserDidEndDocument: method calls this method:
- (void) cleanupShowParsing {
[_buildingShows removeAllObjects];
[_showsParser abortParsing];
[_showsParser release];
_showsParser = nil;
_currentlyBuildingShows = NO;
}
Note that the parser is aborted and released in here. This seems to
work just fine, multiple times, with no problems. However, if I give
it a URL to non-XML data (should be a 404 page somewhere), it calls
parser:parseErrorOccurred: as expected. This method also calls -
cleanupShowParsing. However, when it is called from here, the
program eventually terminates with:
2009-04-30 23:53:52.573 Revision3[49280:20b] PARSE ERROR: Error
Domain=NSXMLParserErrorDomain Code=5 "Operation could not be
completed. (NSXMLParserErrorDomain error 5.)"
objc[49280]: FREED(id): message shouldContinueAfterFatalError sent
to freed object=0xf305a0
I have verified that 0xf305a0 is indeed _showsParser. Further, I've
verified that if I don't release _showsParser in -
cleanupShowParsing, no error occurs given a non-XML file.
My question is it the case that NSXMLParser frees itself if it
encounters an error, but does NOT free itself on a successful parse
of a document?
No Cocoa object (or Objective-C object, for that matter) instance will
release itself to the point of deallocation (except in certain
circumstances of a failed -init). To do so would violate memory
management and object ownership. Something to keep in mind.
If I may propose a rule: never deallocate an object from one of its
delegate method calls unless it is documented as specifically
allowable. You don't know whether the object is complete and won't
call any of its own methods right after the delegate call. So, don't
call -cleanupShowParsing in -parser:parseErrorOccurred:, call it only
from parserDidEndDocument:.
HTH,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden