I’m getting an analyser leak, however this is the same code i’m using elsewhere without a problem. I know I’m using alloc and therefore I have to release, but I am doing this in dealloc.
What am I doing wrong ?
Header file:
@interface myViewController : UIViewController <UITableViewDataSource,
UITableViewDelegate> {
UIBarButtonItem *addButton;
}
@property (nonatomic, retain) UIBarButtonItem *addButton;
Main file:
@synthesize addButton;
- (void)viewDidLoad {
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:@selector(btnNavAddPressed:)];
addButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:addButton];
[tools setItems:buttons animated:NO];
[buttons release];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:tools];
addButton.enabled = FALSE;
- (void)dealloc {
[addButton release];
Both the above answers are misleading. You don’t need to use a setter, it’s perfectly fine to assign objects directly to iVars. You do need to release anything you alloc or retain however. The problem you have is here:
This line is alloc’ing a
UIBarButtonIteminstance and setting it to therightBarButtonItemproperty of thenavigationItem. That means thenavigationItemis retaining theUIBarButtonItemand it is responsible for that retain. You are responsible for releasing it b/c of theallocand you are not. Change the code to this:and this leak goes away.