Reputation: 9545
Why does my app crash when i try to release the image I created with +imageNamed: inside dealloc. My code is as follows:
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
Upvotes: 0
Views: 471
Reputation: 25318
You need to release your stuff first, then call [super dealloc]
. [super dealloc]
will release the classes memory and accessing an ivar afterwards will result in an segfault.
Upvotes: 0
Reputation: 17918
In your dealloc method, you need to move [super dealloc]
to the bottom. You're trying to access your object's instance variable after it's been dealloced.
Upvotes: 3
Reputation: 21760
[UIImage imagedNamed:@"myImage.png""];
is autoreleased. It is also memory managed for you. If you need to release it immediately then alloc/init a UIImage or create UIImageView.
#import <UIKit/UIKit.h>
@interface MyClass{
}
@end
#import "MyClass.h"
@implementation MyClass
-(void)viewDidLoad{
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRect(0,0,100,200)];
imageView.image = [UIImage imagedNamed:@"myImage.png""];
[self.view addSubView:imageView];
[imageView release];
}
-(void)viewDidUnload{
}
-(void)dealloc{
[super dealloc];
}
Upvotes: 0
Reputation: 2567
You might want to review apples memory management guide here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
Particularly the section stating:
You take ownership of an object if you create it using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message.
Since you are not using one of these functions you do not have ownership of the object and therefore can not release it.
Upvotes: -1