为什么当我尝试释放用+ imageNamed创建的图像时,应用程序崩溃:dealloc内部。我的代码如下:当使用+ imageNamed创建的uiimage在dealloc中释放时,应用程序崩溃
MyClass.h:
#import <UIKit/UIKit.h>
@interface MyClass{
UIImage *_thumbImage;
}
@property (nonatomic,retain) UIImage *thumbImage;
@end
MyClass.m:
#import "MyClass.h"
@implementation MyClass
@synthesize thumbImage = _thumbImage;
-(void)viewDidLoad{
[super viewDidLoad];
self.thumbImage = [UIImage imagedNamed:@"myImage.png""];
}
-(void)viewDidUnload{
self.thumbImage = nil;
}
-(void)dealloc{
[super dealloc];
[_thumbImage release]; //if i leave this here, the app crashes. should i release my property?
}
@end
thx它的工作。所以应该在dealloc方法的结尾总是调用[super dealloc]?我认为超级dealloc只是删除父类中的实例变量,为什么它会影响我的子类中的Ivars的保留数? – prostock 2011-04-08 17:25:20
[super dealloc]不会更改实例变量的保留计数。它告诉操作系统你的对象的内存可以被释放。所以,当你尝试访问实例变量后,你引用了你不再拥有的内存。 – cduhn 2011-04-08 17:36:19
...所以是的。总是最后调用[super dealloc]。 – cduhn 2011-04-08 17:37:03