At 11:44 -0500 25/2/09, Michael Ash wrote:
On Wed, Feb 25, 2009 at 12:00 AM, Peter N Lewis <pe...@stairways.com.au> wrote:
 The Application Kit creates an autorelease pool on the main thread at the
 beginning of every cycle of the event loop, and drains it at the end,
 thereby releasing any autoreleased objects generated while processing an
 event.

 This is assuming iPhone or Leopard, I believe the NSAutoreleasePool creation
 was required for older versions of Mac OS X.

The semantics haven't changed on Leopard as far as I know. The above
has been the case forever: pools are created and destroyed on
*events*, but not timers. If you simply run a timer forever and never
process events, your memory usage will grow without limit. A pool
exists, so you don't explicitly leak, but the pool is only drained
when an event arrives. If you're going to be running something in
timers or other non-event sources, you'll want to post a fake event to
the main event loop from time to time (or just after every action you
take) to force the pool to drain.

I have *no* idea how much of that, if any, applies to the iPhone. In
any case if you're processing events on a regular basis as well then
you don't have to worry about anything.

OK, I will certainly defer to Michael's deeper knowledge on this stuff, but I thought Leopard plugged most of the normal case holes for requiring an auto release pool, so I wrote up this test case:

#import "AppDelegate.h"

@interface BogusObject : NSObject {
}

@end

@implementation BogusObject

- (id) init;
{
  self = [super init];
  if (self != nil) {
    NSLog( @"BogusObject init" );
  }
  return self;
}

- (void) dealloc;
{
  NSLog( @"BogusObject dealloc" );
  [super dealloc];
}

@end

@implementation AppDelegate

- (void) timerFired:(NSTimer*)theTimer;
{
  NSLog( @"timerFired" );
  [[[BogusObject alloc] init] autorelease];
}

- (void) applicationDidFinishLaunching:(NSNotification *)aNotification;
{
  NSLog( @"applicationDidFinishLaunching" );
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}

@end


With the app sitting in the background doing nothing, I get the same results whether timerFired uses "release" or "autorelease", ie every second all three messages are printed:

2009-02-26 10:38:43.440 CheckTimer[82997:10b] timerFired
2009-02-26 10:38:43.441 CheckTimer[82997:10b] BogusObject init
2009-02-26 10:38:43.441 CheckTimer[82997:10b] BogusObject dealloc

For good measure, I tried this on Tiger, and got the same results, so now I'm just left wondering where my confusion lies.
   Peter.

--
     Run macros from your iPhone with Keyboard Maestro Control!
       or take a break with Aragom Space War for your iPhone

Keyboard Maestro <http://www.keyboardmaestro.com/> Macros for your Mac
Aragom Space War <http://www.stairways.com/iphone/aragom> Don't get killed!
<http://www.stairways.com/>           <http://download.stairways.com/>
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to