Shivbaba's son
Shivbaba's son

Reputation: 1369

When to release dynamically created objects

In my iPad App,

I have one custom class inherited from UIButton.

I am adding the buttons on my main view dynamically using for loop.

This button has also has label.

My problem is when to release the Custom Class's Objects.

for(NSMutableDictionary *dict  in Categories)
{

Category *category=[[Category alloc] initWithName:[dict valueForKey:@"category_name"]  identity:[[dict valueForKey:@"category_id"] intValue] imageName:nil];

category.lblimagesCount=[[UILabel alloc] initWithFrame:CGRectMake(category.frame.size.width-31,   category.frame.origin.y-42, 26, 26)];

[category addSubview:category.lblimagesCount];

[self.viewHeader addSubview:category];

[category release];
category=nil;

}

How to avoid memory leaks. Especially for 1. labels 2. Category class.

Where to write release ?

i. Category class(UIButton) has its dealloc method but it is never called.

ii. As I am releasing it immediately after adding it to the subview , Will it affect my button click.

iii. When should I release the labels.

Very confused about the memory management.

Upvotes: 0

Views: 95

Answers (1)

Antonio MG
Antonio MG

Reputation: 20410

I'll answer your questions:

i. Category class(UIButton) has its dealloc method but it is never called. Because its not totally released, see question ii.

ii. As I am releasing it immediately after adding it to the subview , Will it affect my button click. No, because your subview is now the owner of the button, when you add it to the subview, both you and the subview are owners, when you release, the button now is owned by the subView.

iii. When should I release the labels. As soon as you add then to the subview, for the same reason as the question ii.

I'll be happy to clarify if you dont understand it.

Upvotes: 3

Related Questions