Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6701137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:53:52+00:00 2026-05-26T06:53:52+00:00

I am trying to follow the XMLPerformance example to make an xml parser of

  • 0

I am trying to follow the XMLPerformance example to make an xml parser of my own. So far I’m having the hardest time making autorelease pools work, I get a crash the instant I recreate a pool.

I narrowed down the issue to this test case:

PoolCrashTest.h

#import <SenTestingKit/SenTestingKit.h>

@interface PoolCrashTest : SenTestCase
{
  @private
  NSURLConnection *connection;
  NSAutoreleasePool *downloadAndParsePool;
  BOOL done;
}

@property (nonatomic, retain) NSURLConnection *connection;
@property (nonatomic, assign) NSAutoreleasePool *downloadAndParsePool;

- (void)downloadAndParse:(NSURL *)url;
@end

PoolCrashTest.m

#import "PoolCrashTest.h"

@implementation PoolCrashTest

@synthesize downloadAndParsePool, connection;

- (void)downloadAndParse:(NSURL *)url {
  done = NO;
  self.downloadAndParsePool = [[NSAutoreleasePool alloc] init];
  NSURLRequest *theRequest = [NSURLRequest requestWithURL:url];
  self.connection = [[NSURLConnection alloc] 
                     initWithRequest:theRequest delegate:self];
  if (connection != nil) {
    do {
      [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode 
                               beforeDate:[NSDate distantFuture]];
    } while (!done);
  }
  self.connection = nil;
  [downloadAndParsePool release];
  self.downloadAndParsePool = nil;
}

#pragma mark NSURLConnection Delegate methods

- (void)connection:(NSURLConnection *)connection 
    didReceiveData:(NSData *)data {
  [downloadAndParsePool drain];

crash after this line ^

  self.downloadAndParsePool = [[NSAutoreleasePool alloc] init];
}

- (void)testPoolCrash
{
  NSURL *dumpURL = [NSURL URLWithString:@"file:///some.xml"];

  [NSThread detachNewThreadSelector:@selector(downloadAndParse:) 
                               toTarget:self withObject:dumpURL];
  sleep(10);
}

@end

Can someone explain how to properly purge autorelease pool in a NSURLConnection delegate running in a thread?

I have tried to follow XMLPerformance as close as possible… I’m targeting Lion with mostly default project settings.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:53:52+00:00Added an answer on May 26, 2026 at 6:53 am

    craig is correct when he says you’re over-releasing your pool. In a non-GC environment, release and drain have the same effect. In a GC environment, release is a no-op for any object, so drain must be used instead. I’d just use drain.

    However, NSAutoreleasePool objects aren’t really things you should be making a property of your class; they’ll work best for you if you restrict their use to a lexical scope. There are a couple of ways you could use a pool in the code you posted above which would be sufficient.

    Remember when you’re spinning your run loop that it’s going to be popping in and out of the call to run the run loop in the common modes; so you could do this instead:

    if (connection != nil) {
      do {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode 
                                 beforeDate:[NSDate distantFuture]];
        [pool drain];
      } while (!done);
    }
    

    and you’d be draining any autoreleased objects created in the particular turn of the run loop. Because the connection’s delegate callback will be called due to this invocation of the run loop, any autoreleased objects created in the delegate callback will be cleaned up when this pool drains.

    If you’re not comfortable with this, you could place a pool inside your delegate method depending on how much work your delegate method is likely to be doing:

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        // Do whatever work you want here
        [pool drain];
    }
    

    And in your case, it would have roughly the same effect.

    I would strongly recommend doing something like my first example above and eliminating the autorelease pool behavior you have right now. Keeping NSAutoreleasePool objects to a single lexical scope facilitates everything from debugging to safe exception handling when it becomes necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

trying to follow example in the expert f# book, and having an issue with
Trying to follow this example to make it work: http://weblogs.asp.net/kiyoshi/archive/2008/10/08/wcf-using-webhttpbinding-for-rest-services.aspx Here is my App.config
Trying to follow this example. (Section String sorting...) Is there anything obvious that would
I'm trying to follow a simple example of AJAX-enabled WCF Service like: http://www.pluralsight.com/community/blogs/fritz/archive/2008/01/31/50121.aspx I'm
I am trying to follow this example (from p137 of Rob Pickering's Foundations of
Iam trying to follow the multithreading example given in: Python urllib2.urlopen() is slow, need
I am trying to follow this example from MSDN: http://msdn.microsoft.com/en-us/library/a1hetckb.aspx I think i'm doing
I am trying to follow the short example in the following answer on using
Im trying to follow this article for having simultaneous pan and pinch gesture recognizers
I am trying to follow a tutorial about xml parsing http://www.edumobile.org/iphone/iphone-programming-tutorials/parsing-an-xml-file/ . The tutorial

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.