【发布时间】:2012-08-08 08:52:45
【问题描述】:
如果这是一个愚蠢的问题,我深表歉意,但我对 iOS 开发非常陌生。那么问题来了:
当我将SecondView 添加到FirstView 时,内存已分配,但当我删除它时,并非所有分配的内存都被释放。
代码如下:
第一视图:
@interface FirstView : UIViewController
@property (nonatomic, retain) SecondView *secondView;
- (IBAction)loadSecondView;
@end
@implementation ViewController
@synthesize editView;
- (IBAction)loadSecondView{
secondView = [[SecondView alloc]init];
[self presentModalViewController:editView animated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
[secondView release]; secondView = nil; //this is called after SecondView is removed
}
第二视图:
@interface SecondView : UIViewController
@end
@implementation EditView
-(void)viewDidLoad{
for (int intCounter = 0; intCounter < 10000; intCounter++){
UIImageView *image = [[UIImageView alloc]init]; //create 10000 images just to fill up the memory
[self.view addSubview:image];
[image release]; image = nil;
}
}
@end
以下是我从 Instruments (Live Bytes) 获得的一些数字:
- FirstView 已加载:671 KB
- 已加载第二视图:3.28 MB
- SecondView 已删除:809 KB
注意:仪器显示没有泄漏
Live 字节不应该成为初始值(671 KB)吗?剩下的是什么而不是 809 KB?
【问题讨论】:
-
你为什么不使用arc?如果你这样做,你不应该担心释放东西。 (仅将指针设置为 nil,其余部分由操作系统处理)
-
我刚刚将项目迁移到arc,仍然遇到同样的问题。
-
有什么问题?没有泄漏意味着它是好的。一些内存可能用于缓存、单例等
标签: ios memory-management uiviewcontroller