iphone - Problem with memory manament in Objective-C -
i have method:
-(nsarray *)dosomething{ nsarray *array = [[nsarray alloc] initwithobjects:@"huy 1",@"huy 2",@"huy 3",nil]; [array release]; return array; }
and
- (void)viewdidload { [super viewdidload]; nsarray *array = [self dosomething]; if(array&&array.count>0){ nslog([nsstring stringwithformat:@"%@\n",[array objectatindex:1]]); } else{ nslog(@"null"); } }
i thinks released array on dosomething() won't return nsarray created on dosomething(). don't know still print "huy 2"? can tell me why?
-(nsarray *)dosomething { nsarray *array = [[nsarray alloc] initwithobjects:@"huy 1",@"huy 2",@"huy 3",nil]; return [array autorelease]; }
memory management programming guide
- (void)viewdidload { [super viewdidload]; nsarray *array = [self dosomething]; if([array count]) //if array nil evaluate false. if count 0 evaluate false too. { nslog(@"%@\n", [array objectatindex:1]); //nslog insert values } else { nslog(@"null"); } }
if works release
instead of autorelease
it's because memory array using has yet used else. chance. suspect if allocate object after nsarray *array = [self dosomething];
unexpected results.
Comments
Post a Comment