Re: Still learning about NSAutoReleasePool
Re: Still learning about NSAutoReleasePool
- Subject: Re: Still learning about NSAutoReleasePool
- From: Eric Brunstad <email@hidden>
- Date: Sat, 11 Jun 2005 11:58:04 -0400
Hi,
On Jun 11, 2005, at 8:46 AM, Theodore H. Smith wrote:
OK, I did this experiment with a new blank Cocoa NSDocument based
project.
I changed one method, this one below:
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
NSString* s = [NSString stringWithCString:"hello"];
[s autorelease];
[s autorelease];
[s autorelease];
}
I ran the project, and the project did not complain or crash. The
window was displayed just fine. Strange.
I'm assuming that the NSString* s actually got corrupted, but
because the behaviour is undefined upon corruption, it just so
happened that I was unlucky enough that no crash occurred. (I think
it is a bad thing for bugs to pass unseen, which is why I say
unlucky).
OK, so assuming that, then it also appears that NSAutoReleasePool
does not check for overreleasing, when it actually could.
You are wrong. Here's what's really happening:
1) windowControllerDidLoadNib is called and an autorelease pool is
put into effect.
2) -stringWithCString returns a NSString which is added to the
autorelease pool.
3) your subsequent calls to -autorelease do nothing because the
object is already in the autorelease pool.
4) -windowControllerDidLoadNib ends and the autorelease pool is
released, therefore deleting the string that you created.
Eric Brunstad
Mind Sprockets Software
email@hidden
www.mindsprockets.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden