ios - How to release memory for declared object in iPhone -
this question has answer here:
- app crash in dealloc 2 answers
i'm facing small problem,
i declare array in .h file , allocate in viewdodload
method. in dealloc
method check if array not equal nil
array=nil
. it's crashing in ios 5.1.1. can't understand reason crash.
my code,
@interface sampleapp : uiviewcontroller { nsmutablearray *objarray; } @end @implementation sampleapp - (void)viewdidload { [super viewdidload]; objarray=[[nsmutablearray alloc]init]; } -(void)dealloc { [super dealloc]; if (objarray!=nil) { [objarray removeallobjects]; [objarray release];objarray=nil; } }
add [super dealloc];
@ end of dealloc
method , not @ beginning. recommended apple in documentation dealloc
method.
when not using arc, implementation of dealloc must invoke superclass’s implementation last instruction.
modify code below,
-(void)dealloc { if (objarray!=nil) { [objarray removeallobjects]; [objarray release];objarray=nil; } [super dealloc]; }
also, need not call [objarray removeallobjects]
when releasing entire array. when array released, internally call release
on contained objects.
hope helps!
Comments
Post a Comment