爲什麼當我嘗試釋放用+ 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