我正在为 iPad 开发一个应用程序。它提供了几个视图,所以我必须小心内存管理。
我的问题与自动释放对象有关。我想将 NSAutoreleasePool 关联到每个视图控制器。像这样的东西:
MyViewController.h
@interface MyViewController: UIViewController
@property (nonatomic, retain) NSAutoreleasePool *myPool;
我的视图控制器.m
@implementation MyViewController
@synthesize myPool;
- (void) viewDidLoad {
myPool = [[NSAuroteleasePool alloc] init];
}
- (void) dealloc {
[myPool drain];
}
NSAutoreleasePool 不能用作属性。我想实现与此类似的行为。任何想法?先感谢您。
编辑
谢谢您的回答。回答您的问题(我还不能回答我的问题):
viewController 会做更多的事情,会响应事件等等。我想要的是,在所有这些操作之后,释放应该自动释放的东西。扩展示例:
MyViewController.h
@interface MyViewController: UIViewController
@property (nonatomic, retain) NSAutoreleasePool *myPool;
我的视图控制器.m
@implementation MyViewController
@synthesize myPool;
- (void) viewDidLoad {
myPool = [[NSAuroteleasePool alloc] init];
}
- (IBAction) whatEver: (id) sender {
UIImage *img = [UIImage imageWithData: ...];
NSString *str = @"MyString";
...
}
- (void) dealloc {
[myPool drain];
}
在这里,字符串和图像会发生什么?我猜他们被保留在游泳池里,不是吗?我可以在 main 方法中等待释放池,但我想它会在应用程序结束时被耗尽。