• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSXMLParser frees itself on error?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSXMLParser frees itself on error?


  • Subject: Re: NSXMLParser frees itself on error?
  • From: Jeff Johnson <email@hidden>
  • Date: Wed, 6 May 2009 17:49:23 -0500

On May 4, 2009, at 1:31 PM, Stephen J. Butler wrote:

On Fri, May 1, 2009 at 11:18 AM, Jesper Storm Bache <email@hidden> wrote:
I have not used NSXMLParser, but as far as I can tell from your email,
cleanupShowParsing is called in response to a call to the parser delegate.
In that case, you should not release the parser (because it is calling your
delegate and is on the stack).
Try using autorelease instead.

That could also be dangerous. What if the parser sets up its own autorelease pool while walking the XML document? You could have a situation like this:

- (void) parse {
 [self doSomething];

 while (notDone) {
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [delegate callbackMethod];

   [pool drain];
 }

 [self doSomethingMore];
}

What you're suggesting would put the parser into the inner autorelease pool!

I still maintain that it's never safe to release/autorelease an object
from inside one of it's delegate calls. If it works at all, you're
implicitly relying on an implementation detail that's subject to
change.

I find this idea somewhat suspect. It seems to undermine the basic function of the autorelease call, which is delay a release until later.


The reasoning here could be taken to the point of absurdity. This is because a 'callee' (self, that is) never knows whether the caller has wrapped the called method in an inner autorelease pool. Thus, the callee never knows whether its own calls to autorelease will be subsequently turned into releases by the drained pool immediately upon returning from the called method. Is the callee never supposed to use autorelease, to be safe?

From the callee's perspective, a call to autorelease defers the release until the end of the event loop. If the caller does anything to shorten the lifetime of objects, that's the callers own fault and responsibility.

In my opinion, the above code is very questionable. You're just asking for trouble if you create an autorelease pool yourself and then call out to the unknown, especially to an object for which you only have a weak reference and who is probably your owner, the delegate. You are purposely shortening the lifetime of objects, so you're responsible for making sure nothing important gets deallocated prematurely.

If you really must use code such as the above, I think you should do [[self retain] autorelease] before creating the pool.

-Jeff

_______________________________________________

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


  • Follow-Ups:
    • Re: NSXMLParser frees itself on error?
      • From: "Stephen J. Butler" <email@hidden>
    • Re: NSXMLParser frees itself on error?
      • From: Marcel Weiher <email@hidden>
References: 
 >NSXMLParser frees itself on error? (From: Mike Manzano <email@hidden>)
 >Re: NSXMLParser frees itself on error? (From: Jesper Storm Bache <email@hidden>)
 >Re: NSXMLParser frees itself on error? (From: "Stephen J. Butler" <email@hidden>)

  • Prev by Date: Re: Vector Graphic Dock Icon?
  • Next by Date: Re: Custom tableView cell by subclassing NSCell
  • Previous by thread: Re: NSXMLParser frees itself on error?
  • Next by thread: Re: NSXMLParser frees itself on error?
  • Index(es):
    • Date
    • Thread