我的财产是否泄露记忆?

时间:2012-04-18 15:33:25

标签: iphone objective-c

- (void)viewDidLoad //In this scenario it only gets called once, but in other bits of code with same property initialisation it might be called more than once
{
deleteButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(deleteModelTapped:)]; //Is this leaking?
    self.deleteButton.image = [UIImage imageNamed:[Configuration getDeleteIconName]];
}

@property (nonatomic, retain) IBOutlet UIBarButtonItem *deleteButton;

- (void)dealloc 
{    
    [deleteButton release];
    [super dealloc];
}

1 个答案:

答案 0 :(得分:2)

不,但是这样写的可能更好

- (void)viewDidLoad
{
    self.deleteButton = [[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(deleteModelTapped:)] autorelease]; 
    self.deleteButton.image = [UIImage imageNamed:[Configuration getDeleteIconName]];
}

setProperty展开可能就像这样

- (void)setProperty:(XXX*)p
{
    if ( property != p )
    {
        [property release];
        property = [p retain];
    }
}

“泄漏”可能使用“[UIImage imageNamed:]”; :)