I’ve declared the following variable as an instance variable and am using it in my m file, however I’ve getting a warning.
TransparentToolbar *tools;
Potential leak of an object allocated on line …
I’ have tried creating a property for it, for example..
@property (nonatomic, retain) TransparentToolbar *tools;
And synthesize’ing and releasing it, but my view crashes at the end of dealloc.
What am I doing wrong ?
EDIT same warning on pickerSortingDataCurrent …
h
@interface myViewController : UIViewController <UIActionSheetDelegate,
UIPickerViewDelegate, UIPickerViewDataSource, UITableViewDelegate,
UITableViewDataSource, MFMailComposeViewControllerDelegate> {
TransparentToolbar *tools;
NSArray *pickerSortingDataCurrent;
}
@property (nonatomic, retain) TransparentToolbar *tools;
@property (nonatomic, retain) NSArray *pickerSortingDataCurrent;
m
@synthesize pickerSortingDataCurrent;
@synthesize tools;
- (void)viewDidLoad {
[super viewDidLoad];
tools = [[[TransparentToolbar alloc]
initWithFrame:CGRectMake(0, 0, 70, 44.01)] autorelease];
tools.barStyle = UIBarStyleBlackOpaque;
self.pickerSortingDataCurrent = [[NSArray alloc] initWithObjects:
@"Next Date Ascending",
@"Next Date Descending", nil]; // removed some items here
}
- (void)dealloc {
[tools release];
[pickerSortingDataCurrent release];
[super dealloc];
}
Ahhhh I have autorelease…. but that doesn’t solve the pickerSortingDataCurrent …
EDIT…
#import "TransparentToolbar.h"
@implementation TransparentToolbar
- (void)drawRect:(CGRect)rect {
// do nothing in here
}
- (void) applyTranslucentBackground
{
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
self.translucent = YES;
}
- (id) init
{
self = [super init];
[self applyTranslucentBackground];
return self;
}
// Override initWithFrame.
- (id) initWithFrame:(CGRect) frame
{
self = [super initWithFrame:frame];
[self applyTranslucentBackground];
return self;
}
@end
FURTHER EDIT

If you define
@propertythen generally any time you access the ivar in your class you use the getter/setter whether that be dot notation or standard method call.Dot notation
Standard method call
If you always explicitly use these getters and setters then you pretty much do not need to call release anywhere in your code apart from
deallocor an overriddensetMyVar:method. Doing it this way allows memory management to occur in limited places. If you start releasing and retaining yourself then things can be a bit complicated when you first start out.UPDATE
@bbum gives you the answer but I think you would benefit from being more consistant in your coding as well.
For example before the offending line you are assigning directly to an ivar without using the setter. Be consistant and use the setter/getter that you took the time to synthesize. I would rewrite
to
Your
initmethods are not really following the guidelines either you should be checking thatselfis actually set, so it should look something similar to:UPDATE
The memory leak you are seeing here:
is because you look at the docs for UINavigationItem you will see that
rightBarButtonItemis declared asretainTherefore the call
self.navigationItem.rightBarButtonItemwill take a +1 retain on the object you pass in and then you are alloc/initing which is another +1 retain. TheUINavigationItemwill release it’s retain when it is dealloc’d but there will still be your original retain hanging around.The fix: