2012-04-18 84 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