Kingkong
Kingkong

Reputation: 27

UIViewControllers... Memory Management With SubViews

I have UIViewController who has several subviews but tracking each and every subview allocated is difficult task as the actually coder is not me and need to handle the memory consumption.

My question is there a way to control the memory calling a recursive function to remove and release all the subViews in the UIViewController without knowing the actual reference name ?

As in the code below :

for (UIView* subview in view){  
    [subview removeFromSuperView];  
    [subview release] ;
    subview = nil;   
}

Upvotes: 0

Views: 509

Answers (1)

lxt
lxt

Reputation: 31304

When you call removeFromSuperview on a view it will automatically decrement the retain count (because the superview no longer requires a reference to the view you've just removed).

If you have added your views to the superview in a standard manner there shouldn't be any need to do what you're doing - either you've added your views and then released them, or your views are properties and the release happens later.

The code you're proposing (the recursive loop on all subviews) is a bad idea, because you don't actually know whether your subview is safe to be released or not. You could easily trigger a bad access.

Upvotes: 2

Related Questions