Reputation: 31
Sir,
- (void)viewDidLoad{
for (int i = 1; i <= 4; i++)
{
playButton=[[UIButton alloc]initWithFrame:CGRectMake(curLocX,1.0,45,kScrollObjHeight)];
[playButton setBackgroundImage:[UIImage imageNamed:@"Play.png"] forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(playClicked) forControlEvents:UIControlEventTouchUpInside];
curLocX+=100;
}}
I have release the playButton in viewDidUnload and dealloc 1.I have created four buttons but I released only one time its right or wrong 2.I have released viewDidUnload and dealloc its right or wrong Advance Thanks for your valuable suggestions Suresh.M
Upvotes: 0
Views: 194
Reputation: 4517
yes, if an object has been allocated 4 times, it should be released by the same number of count.
btw am not sure what are you trying to do here, at the end of the loop playButton will only have reference to the last object and you are not even adding your playButton as a subview to any view. Anyways you can avoid using the alloc and user buttonWithType method, this method itself handles the alloc and release of UIButton object so you don't have to worry about the release call.
- (void)viewDidLoad{
for (int i = 1; i <= 4; i++)
{
UIButton *playButton = [UIButton buttonWithType: UIButtonTypeCustom];
playButton.frame = CGRectMake(curLocX,1.0,45,kScrollObjHeight);
[playButton setBackgroundImage:[UIImage imageNamed:@"Play.png"] forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(playClicked) forControlEvents:UIControlEventTouchUpInside];
curLocX+=100;
[self.view addSubview: playButton]; //you might have missed this statement
}}
Upvotes: 2
Reputation: 15628
you have to release the button in the for loop itself.
for (int i = 1; i <= 4; i++)
{
playButton=[[UIButton alloc]initWithFrame:CGRectMake(curLocX,1.0,45,kScrollObjHeight)];
[playButton setBackgroundImage:[UIImage imageNamed:@"Play.png"] forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(playClicked) forControlEvents:UIControlEventTouchUpInside];
curLocX+=100;
[playButton release];
}
NOTE: If you allocated an object 'n' times you have to release the object 'n' times.
Upvotes: 0
Reputation: 8109
you should remove your button release from at least one of the places (viewDidUnload/dealloc) to avoid dubble release getting called for an object.
creating objects (in your case buttons) and releasing all of them at one time is fine.
Upvotes: 0