iphone - IBOutlet, use of member properties or not? memory leak? -
i have noticed memory usage of program wrote, keep increasing on time. xcode's instruments shows no memory leak, yet can see heap stack growing on time..
upon investigation, lot of memory usage come iboutlet ui objects. interface build interface builder.
typical usage like:
header:
@interface helpviewcontroller : uiviewcontroller <uiwebviewdelegate> { iboutlet uiwebview *webview; iboutlet uibaritem *backbutton; iboutlet uibaritem *forwardbutton; nsstring *url; iboutlet uiactivityindicatorview *spin; } @property (nonatomic, retain) nsstring *url;
and usage :
- (void)webviewdidstartload:(uiwebview *)mwebview { backbutton.enabled = (webview.cangoback); forwardbutton.enabled = (webview.cangoforward); [spin startanimating]; } - (void)webviewdidfinishload:(uiwebview *)webview { backbutton.enabled = (webview.cangoback); forwardbutton.enabled = (webview.cangoforward); [spin stopanimating]; }
looking @ heap stack, find uiactivityindicatorview *spin object isn't deallocated, , memory footprint keep growing.
however, if change code to: header:
@interface helpviewcontroller : uiviewcontroller <uiwebviewdelegate> { iboutlet uiwebview *webview; iboutlet uibaritem *backbutton; iboutlet uibaritem *forwardbutton; nsstring *url; uiactivityindicatorview *spin; } @property (nonatomic, retain) nsstring *url; @property (nonatomic, assign) iboutlet uiactivityindicatorview *spin;
and in code do:
synthesize spin; - (void)webviewdidstartload:(uiwebview *)mwebview { backbutton.enabled = (webview.cangoback); forwardbutton.enabled = (webview.cangoforward); [self.spin startanimating]; } - (void)webviewdidfinishload:(uiwebview *)webview { backbutton.enabled = (webview.cangoback); forwardbutton.enabled = (webview.cangoforward); [self.spin stopanimating]; }
nothing more, nothing else, heap stack doesn't grow anywhere much.. , uiactivityindicatorview object doesn't leave stuff behind
i can't figure out why make difference here having assign property or not, doesn't make sense ! unless massively misunderstood what's happening.
any explanations welcomed..
thanks
you need release objects in dealloc method:
-(void)dealloc { [webview release]; [backbutton release]; [forwardbutton release]; [url release]; [spin release]; [super dealloc]; }
the reason why problem doesn't occour in second version is, set property attribute assign, should use retain "object-properties" assign used basic datatypes int, float, bool etc...
edit:
to part retain , assign, afaik behaviour following: if property made assign, setting
self.thatvariable = something;
would same as:
thatvariable = something;
if used retain same as:
[thatvariable release]; thatvariable = [something retain];
so if used assign variables hold pointers objects can't sure, object isnt deallocated somewhere else, result in bad access.
afaik reason use assign object weak reference. if have object both retain each other, none of ever released. thats spot use assign objects. (f.e. in delegate-pattern. object retain it's delegate , delegate retain object. in case delegate assigned)
Comments
Post a Comment