iphone - How to manage memory management in iOS -
i need clarification memory management concepts.
i declare 1 variable in .h
@interface rootviewcontroller : uiviewcontroller { nsmutablearray *objmutablearray; } @property (nonatomic,retain) nsmutablearray *objmutablearray;
in .m file
@implementation rootviewcontroller @synthesize objmutablearray=_objmutablearray; - (void)viewdidload { [super viewdidload]; self.objmutablearray=[[nsmutablearray alloc]init]; [self.objmutablearray addobject:@"firstrow"]; [self.objmutablearray addobject:@"secondrow"]; [self.objmutablearray addobject:@"thirdrow"]; [self.objmutablearray addobject:@"fourthrow"]; [self.objmutablearray addobject:@"fifthrow"]; }
i used self.objmutablearray places. when release memory instance used _objmutablearray.
- (void)dealloc { [_objmutablearray release]; [super dealloc]; }
actually confused when release memory instance. please tell me did correct or must release "objmutablearray" object.
you seem using manual memory management , not arc. fine, got wrong.
please tell me did correct or must release "objmutablearray" object.
of course have release because created using alloc
. how did not correct. leaking memory because in viewdidunload
method (i suppose should viewdidload
instead, shouldn't it!?) assigning retain
property - object have reference count of 2 (one because of + alloc
, 1 because of (retain)
).
now when releasing in - dealloc
, still have reference count of one, class doesn't dispose of ownership, hence memory leak.
solution:
you can use either property or instance variable. don't mix two. approach #1:
_objmutablearray = [[nsmutablearray alloc] init]; // ... [_objmutablearray release];
approach #2:
self.objmutablearray = [[[nsmutablearray alloc] init] autorelease]; // ... self.objmutablearray = nil;
Comments
Post a Comment