iphone - Releasing subviews -
i'm new @ programming ios, , experiencing problem ipad app i'm developing. using splitview controller add subview splitview's detailview every time cell in splitview's rootview tapped. fine until stack gets high , run out of memory. how can release previous subview after new view added stack? or there better way of solving problem?
thanks
to remove view superview:
[view removefromsuperview];
the superview release view @ point. if superview actor owning reference view deallocated. put way, this:
[superview addsubview:view];
causes superview retain view. see blocks of code like:
view = [[viewclass alloc] initwithframe:frame]; // own view [superview addsubview:view]; // superview , both own view [view release]; // superview owns view; // it'll deallocated if // superview ever relinquishes // ownership
so have pointer view valid long view remains in superview. it's subsequently safe post removefromsuperview
it, after use of view explicitly unsafe. view object live between alloc/init , removefromsuperview. it'll deallocated upon being removed.
per normal cocoa reference counting rules, following pretty same drop-in replacement alloc/init , subsequent release:
view = [viewclass viewwithframe:frame]; // view autoreleased object; // autorelease pool owns [superview addsubview:view]; // superview owns view // autorelease pool relinquish ownership automatically, in future...
if haven't done manually affect behaviour, being in normal runloop, things in autorelease pool safe life of current call stack. in case you'd treat view did in manual alloc/init example, possibly having made change save line of code , leave memory management implicit.
Comments
Post a Comment