ios - Regarding memory management in Objective C -
according static analyzer if have following property:
@property (retain, nonatomic) someobject * object; and assign property so:
self.object = [someobject alloc] init]; a leak occurs. makes sense because alloc init adds +1 retain count , retaining property increments retain count. best solution here? typically add autorelease so:
self.object = [[someobject alloc] init] autorelease]; but creates problems me , end on releasing object causing app crash. don't have specific examples right remember had take out autoreleases cause of application crashing. there missing here?
edit: have concrete example of issue running into.
nsmutablearray *newdata = [nsmutablearray array]; //if true showing of items in level of hierarchy , not need show summary button. if (!(contextid.count >= 1 && [[contextid objectatindex:contextid.count - 1] isequal:[nsnull null]]) && contextid.count != 0) { geographypickeritem * firstitem = [[geographypickeritem alloc] init]; firstitem.primarystring = [nsstring stringwithstring:@"summary"]; firstitem.substring = [nsstring stringwithstring:@""]; firstitem.issummaryitem = yes; [newdata addobject:firstitem]; [firstitem release]; //todo: figure out why causing exc_bad_access errors } self.hierdata = newdata; the code above in init method of viewcontroller. hierdata retained property, released in viewcontrollers dealloc method. geographypickeritem retains 2 strings, primarystring , substring , releases them in own dealloc method. application crashes (sometimes) when viewcontrollers de-alloced following pop off of navigation controller. crashes exc_bad_access signal in dealloc method of geographypickeritem (either on [substring release] or [primarystring release]).
i don't understand why happening because believe following proper memory management guidelines. if comment out firstitem release fine.
the autorelease method mention fine, other common idiom of:
someobject *thing = [[someobject alloc] init]; self.object = thing; [thing release]; if end overreleasing later on, that problem. part, you're apparently doing correctly, not problem.
Comments
Post a Comment