rptwsthi
rptwsthi

Reputation: 10172

When should we release an object if we are returning it?

Check the following method:

-(NSMutableArray*)provideRequestArray{
    NSMutableArray* requestArray=[[NSMutableArray alloc] initWithObjects:@"MyString",nil];
    return requestArray;
}

Now when should requestArray be released so it doesn't produce any consequences.

Upvotes: 3

Views: 84

Answers (1)

user756245
user756245

Reputation:

Return that object sending an autorelease message.

// initWithFormat: ??
NSMutableArray* requestArray=[[NSMutableArray alloc]
                              initWithFormat:@"MyString"];
return [requestArray autorelease];

or get an autoreleased one (for instance with array class method) :

NSMutableArray* requestArray= [NSMutableArray array];
return requestArray;

Check out the doc here.

Upvotes: 5

Related Questions