We’re building a custom keyboard with the essence of the following code.
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *numericButton;
for (int i = 1; i < 13; i++) {
numericButton = [UIButton buttonWithType:UIButtonTypeCustom];
int xorig, yorig;
int w = BUTTON_SIZE_W_EDG;
int tag = i;
numericButton.frame = CGRectMake(xorig, yorig + BUTTON_OFFSET, w, BUTTON_SIZE_H);
numericButton.tag = tag;
[numericButton addTarget:self
action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:numericButton];
}
}
My question is whether reassigning numericButton to a new UIButton is causing a memory leak? buttonWithType is returning an autoreleased UIButton but I’m not sure whether the previous numericButton ever gets released with this code. Would it make any difference putting the declaration inside the for loop?
thanks
This code does not leak. As you said
buttonWithTypereturns an autoreleased object. Since you do notalloc,copy,neworretain, you don’t claim ownership of anything and you don’t need to (auto)release anything.It would not make any difference to put the declaration in the loop. But it would be the natural place to put it.