Reputation: 1477
I wanted to know that if I am doing correctly, regarding retain and release of digitbutton reference down below. In the book "Programming in Objective C by Stephen Kochan", it says in one section with different example that like digitbutton is holding reference to same object that sender is holding, so in between sender may get released elsewhere and then I would be calling currenttitle method on deallocated digitbutton, if that is the case.
So here I called retain on digitbutton and after its use I called release on it. Well, the example maybe not genuine for this purpose but is this concept valid? Should I make practice to retain objects like in example below?
I am not experienced in writing managed codes.
- (IBAction)clickDigit:(id)sender {
UIButton *digitButton = (UIButton*)sender;
[digitButton retain];
NSLog(@"%@",[digitButton currentTitle]);
[[self displayOutput] setText:[digitButton currentTitle]];
[digitButton release];
}
Yes, the example was wrong; let's say if instead of id sender there is NSString and instead of UIButton, same NSString, and return type is just void. Then do I have to retain sender NSString for safety in case someone holding its reference releases it.
Note: I am not using ARC.
Upvotes: 0
Views: 119
Reputation: 2310
You dont need to retain & release it.
You can do like this
- (IBAction)clickDigit:(UIButton *)sender
{
NSLog(@"%@",[sender currentTitle]);
[[self displayOutput] setText:[sender currentTitle]];
}
Upvotes: 3
Reputation: 926
What you're doing in clickDigit:
is unnecessary.
Instead of me explaining why it is unnecessary, I will point you directly to where I learned proper memory management techniques: Advanced Memory Management Programming Guide
Upvotes: 0