Marc
Marc

Reputation: 1541

Do I need to remove views from superviews in dealloc?

If I alloc/init a view and add it to a another view in code (I did not use a xib) - do I need to remove it when the containing UIViewController's dealloc message is sent? I have seen this code in certain places, and wondered is it necessary under some circumstances to free memory?

Thanks, Marc

Upvotes: 1

Views: 323

Answers (2)

xda1001
xda1001

Reputation: 2459

If you do this,

UIView *v = [[UIView alloc] init];
[self.view addSubview:v];
[v release];

or

UIView *v = [[[UIView alloc] init] autorelease];
[self.view addSubview:v];

,the v will be released when its parent view release;

When the parent view use addSubview, it will retain the subview, and will release the subview when it is released.

Upvotes: 2

Eugene
Eugene

Reputation: 10045

This isn't necessary. All UIView subclasses hold subviews array, which gets released in the final UIView dealloc message, which releases your views.

Upvotes: 2

Related Questions