LOLgrep
LOLgrep

Reputation: 81

UIView Memory Management

Mediocre stupid question: So in my iphone UI, I have a Start/Stop button which programmatically instantiates a UIView. If the button is in stop state, I alloc initWithFrame a UIView, which was declared in the header file, add it to my superview and then release it. So now the retain count should just be one and it's being held in my superview. If I press the stop button I remove the view from my supperview by calling the removeFromSuperView method and set my UIView object to nil.

So here's the problem I am wondering about: Let's say a user presses the start button, instantiates the view, then quits my program. She runs a bunch of other programs which the device realizes it needs more memory, killing my app and running dealloc on my program. Will that UIView become a memory leak? Or is the superview smart enough to check and see if the children are alive and do something accordingly.

Basically, I have been brought up to only do a bunch of [objectName release] in the dealloc method. Should I do a conditional statement like check if the UIView is not nil, and if so, remove it from the superview?

Thanks

Upvotes: 0

Views: 261

Answers (1)

Kirby Todd
Kirby Todd

Reputation: 11546

A superview retains all views added to it and will call release on all these subviews when it is dealloc'd.

When the OS kills your app it will reclaim all memory associated with it regardless of whether you leaked memory in your app or not.

Upvotes: 1

Related Questions