2012-04-18 80 views
0
- (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]; 
} 
+0

您是否启用了ARC?另外,您可能需要阅读仪器 – 2012-04-18 15:37:05

+0

我没有使用ARC – TheLearner 2012-04-18 15:40:05

+0

我已经尝试过使用仪器和它的混淆 – TheLearner 2012-04-18 15:40:26

回答

2

NOP,但写这样也许更好

- (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:]”; :)

+0

所以,如果这个viewDidLoad反复运行,它不会泄漏,因为该属性总是只保留一次? – TheLearner 2012-04-18 15:45:01

+0

是的,设置属性将自动释放前变量 – adali 2012-04-18 15:45:49

+0

不要忘记设置你的属性nil viewDidUnload – 2012-04-18 15:57:31