tags:

views:

83

answers:

2

Hello all,

Please consider the following code:

//CallerClass.h
@interface CallerClass : UITableViewController {

    NSMutableArray *dataArray;

}
@property(nonatomic,retain) NSMutableArray *dataArray;
-(void) setData;

//CallerClass.m
@implementation CallerClass
@synthesize dataArray;

-(id)initWithStyle:(UITableViewStyle)style {

    if (self = [super initWithStyle:style]) {
    [self setData];
   }
    return self;
}

-(void) setData
{
   dataArray = [CalledClass getData];
   [dataArray release];

}


//CalledClass.h
@interface CalledClass : NSObject {

}
+(NSMutableArray*) getData;

//CalledClass.m
@implementation CalledClass

+(NSMutableArray*) getData
{
   NSMutableArray* tempArray = [[NSMutableArray alloc] init];
   return tempArray;
}

I want to know what is the retain count for dataArray that is tempArray. Is it getting released. I dont want to use autorelease as I dont know till what time period I will need it. So I want to release it on my own. When I allocated tempArray, its retain count becomes 1. But when I assign it to instance variable dataArray whose property is retain, Is the retain count for that array becomes 2 or it stays 1. Like on releasing dataArray will it release memory.

+1  A: 

dataArray = [CalledClass getData]; That doesn't invoke the retain attribute of the property. That's just plain old assignment iirc. [self setDataArray:[CalledClass getData]] would give a reference count of 2 on your array.

what about self.dataArray = [Called getData];
rkb
+2  A: 

You've set up your property to retain the value, but then you're not using the accessor methods but set the instance variable directly instead:

dataArray = [CalledClass getData];

won't manage the retain count for you. You have to use:

self.dataArray = [CalledClass getData];

Also, in your CalledClass, I'd change the getData method to this:

+(NSMutableArray*) getData
{
   NSMutableArray* tempArray = [[NSMutableArray alloc] init];
   return [tempArray autorelease];
}

Normally I'd expect to get an autoreleased object back from a method with a name like this.

setData: should then be something like:

-(void) setData
{
   self.dataArray = [CalledClass getData];
}

or you could get rid of it entirely and just directly do

self.dataArray = [CalledClass getData]

in initWithStyle:.

By calling self.dataArray instead of assigning directly to the instance variable, your dataArray property will take care of retaining and releasing the object (because you specified "retain" in the property declaration)

Thomas Müller
So if I am not using autorelease and use self.dataArray = [CalledClass getData], what will be the retain count. How many times I need to release the data.
rkb
[[NSMutableArray alloc] init] will give you a mutable array with retain count of 1. self.dataArray = [CalledClass getData] will increase the retain count to 2. You should then release it once.
Thomas Müller
Ohkk Tnx Thomas. It means if I am not using self then it will not increase the retain count.
rkb