这是我的代码,应用程序在更改为其内部视图时运行。当您多次更改此视图时,它会导致colourButtonsArray内存泄漏(所以不是第一次运行它)但我不确定如何摆脱它:
-(void)setColours {
colourButtonsArray = [[NSMutableArray alloc] init];
[colourButtonsArray addObject:@""];
int buttonsI = 1;
while (buttonsI < 7)
{
//Make a button
UIButton *colourButton = [UIButton buttonWithType:UIButtonTypeCustom];
colourButton.frame = CGRectMake((53*(buttonsI-1))+3, 5, 49, 49);
colourButton.tag = buttonsI;
[colourButton addTarget:self action:@selector(colourButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[colourView addSubview:colourButton];
[colourButtonsArray addObject:colourButton];
[colourButton release];
buttonsI++;
}
}
答案 0 :(得分:0)
你在哪里发布colourButtonsArray
?
如果您不止一次致电setColours
,您将为colorButtonsArray
创建一个新数组并每次泄漏旧数组(假设您只在dealloc方法中释放colourButtonsArray
,或者如果你根本不释放它。)
答案 1 :(得分:0)
正确使用访问者,并根据需要锁定。这可能有所帮助:
-(void)setColours {
/* lock if necessary */
self.colourButtonsArray = [NSMutableArray array];
[self.colourButtonsArray addObject:@""];
int buttonsI = 1;
while (buttonsI < 7)
{
/* Make a button */
UIButton *colourButton = [UIButton buttonWithType:UIButtonTypeCustom];
colourButton.frame = CGRectMake((53*(buttonsI-1))+3, 5, 49, 49);
colourButton.tag = buttonsI;
[colourButton addTarget:self action:@selector(colourButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.colourView addSubview:colourButton];
[self.colourButtonsArray addObject:colourButton];
// no release here: [colourButton release];
buttonsI++;
}
/* unlock if necessary */
}