【发布时间】:2012-05-11 03:11:17
【问题描述】:
我是 Objective-C 的新手,我不确定我是否以正确的方式使用 NSAutoreleasePool。
-
如果我只想使用一次自动释放:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *newText = [NSString stringWithFormat:@"%d", prograssAsInt]; sliderLabel.text = newText; [pool release]; //newText will be released -
如果我想多次使用 autorelease,我会使用:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *newText = [NSString stringWithFormat:@"%d", prograssAsInt]; sliderLabel.text = newText; [pool drain]; //newText will be released newText = [NSString stringWithFormat:@"%d", prograssAsInt]; sliderLabel.text = newText; [pool drain]; //newText will be released newText = [NSString stringWithFormat:@"%d", prograssAsInt]; sliderLabel.text = newText; [pool release]; //newText will be released
这样好吗?有没有内存泄漏?
【问题讨论】:
标签: objective-c ios memory-management autorelease