If I have
ClassA.h
1.@property (nonatomic, retain) NSMutableArray *arr;
ClassA.m
-(ClassA) function {
1.ClassA *obj = [[ClassA alloc] init]; // initialize the arr // add another object into the arr 2. [obj.arr addObject:nameOfAnObject] 3. return obj;}
at ClassB, I want to call (ClassA) function method.What I do is
ClassB.m
-(void)viewDidload {
1.[super viewDidLoad]; 2.ClassA *classAinB = [[classA alloc] init]; 3.classAinB = [classA function]; 4.[classAinB release];}
According the rule of memory management,because I own a ClassAinB in ClassB.m, so I free it at the end.
My question is how is the var ClassA which I own in ClassA.m, when should I release it so that after a call back at line 3 of ClassB, I still have the object of ClassA whose arr is containing nameOfAnObject object
Please advice me on this issue. Any comments are welcomed here. Thanks
Add it to the autorelease pool. i.e.
return [obj autorelease];. This will make sure the object is sent areleasemessage at the end of the run loop iteration if it no longer has any owner.