0

I have uibutton as property and releasing it in dealloc. I am just using it to hide or unhide but the frequency of its usage is quite high.

@property (retain, nonatomic) IBOutlet UIButton *object;

-(void)onsomebuttonclick
{
 object.hidden=true;
}

- (void)dealloc {
 [object release];
}

- (void)viewDidUnload
{
   [self setObject:nil];
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Arun jalota
  • 252
  • 2
  • 14

2 Answers2

3

Why not just run

(a) the static analyser

(b) Instruments with the leak tool enabled

or

(c) use ARC?

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
1

That should work fine. As a note, you should set self.object = nil; instead of releasing it to prevent accidental use of the released object.

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
  • As it's got an IBOutlet we can assume that's a UIViewController. Without releasing the property in viewDidUnload it might leak the next time that viewDidLoad is called. – deanWombourne Oct 15 '12 at 12:20
  • The retain attribute tells it to release when the property is set to a new value, so that should be fine. – Peter DeWeese Oct 15 '12 at 12:22
  • And calling a setter in `dealloc` is not recommended - what if another object is watching that property with KVO and tries to use the, now half dealloced, object? And we don't know if there are any other side effects of a custom setter. – deanWombourne Oct 15 '12 at 12:22
  • The retain attribute might not be called when a view is created from a xib - it might just set the member vars directly without going via the property. – deanWombourne Oct 15 '12 at 12:22
  • Actually, use has been mixed even in Apple examples, as it is logically the same, that is, it is released before being nilled. – Peter DeWeese Oct 15 '12 at 12:26
  • @deanWombourne so what you suggest to do? i already have tried clang analyzer and instruments, and its not showing any leaks there but just wanted to know the right way if doing it? – Arun jalota Oct 15 '12 at 12:26
  • @PeterDeWeese That's a fair point - I do wish Apple were more consistent :) Personally, I like not setting it to nil as it tells me when I have a bug. However, other people completely disagree and `nil` it to make their apps safer after they have been released - it's an ongoing debate that will probably never be resolved! However, there are good reasons not to let the property deal with that so the consensus is `[obj release]; obj=nil;` is better than `self.obj=nil;`. – deanWombourne Oct 15 '12 at 12:32
  • @arunjalota If nothing is telling you there is a leak then why are you so worried? Maybe there isn't a leak! Is there something specific that's happening that makes you think you have a bug? – deanWombourne Oct 15 '12 at 12:33
  • 1
    @deanWombourne, I think that ARC has settled the debate by literally codifying and hiding the pattern! – Peter DeWeese Oct 15 '12 at 13:15