foundationkit

How does the NSAutoreleasePool autorelease pool work?

我的未来我决定 提交于 2019-12-17 02:58:14
问题 As I understand it, anything created with an alloc , new , or copy needs to be manually released. For example: int main(void) { NSString *string; string = [[NSString alloc] init]; /* use the string */ [string release]; } My question, though, is wouldn't this be just as valid?: int main(void) { NSAutoreleasePool *pool; pool = [[NSAutoreleasePool alloc] init]; NSString *string; string = [[[NSString alloc] init] autorelease]; /* use the string */ [pool drain]; } 回答1: Yes, your second code

How does the NSAutoreleasePool autorelease pool work?

拜拜、爱过 提交于 2019-11-26 15:04:13
As I understand it, anything created with an alloc , new , or copy needs to be manually released. For example: int main(void) { NSString *string; string = [[NSString alloc] init]; /* use the string */ [string release]; } My question, though, is wouldn't this be just as valid?: int main(void) { NSAutoreleasePool *pool; pool = [[NSAutoreleasePool alloc] init]; NSString *string; string = [[[NSString alloc] init] autorelease]; /* use the string */ [pool drain]; } kperryua Yes, your second code snippit is perfectly valid. Every time -autorelease is sent to an object, it is added to the inner-most