はい、そのコードには問題があります。imgViewのリリースが早すぎるため、まれにクラッシュが発生する可能性があり、オブジェクトを保持せずにインスタンス変数に格納し、一般的にメモリ管理を間違った方法で行っています。
これを行う1つの正しい方法は次のとおりです。
@interface SomeViewController : UIViewController
{
UIImageView *imgView;
}
@property (nonatomic, retain) UIImageView *imgView;
そして実装では;
@synthesize imgView;
モジュールのどこかに:
//Create a new image view object and store it in a local variable (retain count 1)
UIImageView *newImgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
newImgView.image = [UIImage imageNamed:@"someimage.png"];
//Use our property to store our new image view as an instance variable,
//if an old value of imgView exists, it will be released by generated method,
//and our newImgView gets retained (retain count 2)
self.imgView = newImgView;
//Release local variable, since the new UIImageView is safely stored in the
//imgView instance variable. (retain count 1)
[newImgView release];
//Add the new imgView to main view, it's retain count will be incremented,
//and the UIImageView will remain in memory until it is released by both the
//main view and this controller. (retain count 2)
[self.view addSubview:self.imgView];
そして、dealloc は同じままです。
- (void) dealloc
{
[imgView release];
[super dealloc];
}