PDA

View Full Version : some questions about memory management




david.david
Mar 30, 2009, 10:20 AM
/*
a method of ClassA
here the returned Object is allocated by user
*/
-(NSArray *)classA_Method_1{

NSMutableArray *array = [[NSMutableArray alloc] init];
//here adding some data from sqlite table..
//processing data and add to array dynamically
[array addObject:@"data1"];


return array;
}

/*
a method of ClassA
here the returned Object is allocated by system
*/
-(NSArray *)classA_Method_2{


//here assigning some data from sqlite table..
const char* str1 = "a";
const char* str2 = "b";

NSArray *array = [NSArray arrayWithObjects:[NSString stringWithUTF8String:str1],[NSString stringWithUTF8String:str2], nil] ;


return array;
}


case 1.
/**
the following is the code from a UIViewController sub class
arrayForTableDisplay is the NSArray reference and it is applied the @property(nonatomic, retain) in .h file, and use @synthesized arrayForTableDisplay in .m file
and in dealloc method, also coded [arrayForTableDisplay release];
**/
ClassA *classaObject = [[ClassA alloc] init];
self.arrayForTableDisplay = [classaObject classA_Method_1];
[classaObject release];

question ?
it is okey. right ? if not please tell me !!

case 2.
/**
the following is the code from a UIViewController sub class
arrayForTableDisplay is the NSArray reference and it is applied the @property(nonatomic, retain) in .h file, and use @synthesized arrayForTableDisplay in .m file
and in dealloc method, also coded [arrayForTableDisplay release];
**/
ClassA *classaObject = [[ClassA alloc] init];
self.arrayForTableDisplay = [classaObject classA_Method_2];
[classaObject release];

question ?
in classA_Method_2 do I need to return the array with autorelese code, and self.arrayForTableDisplay = [[classaObject classA_Method_2] retain]; ?
whats the correct way ? please help...


case 3
/**
the following is the code from UIViewController sub class
whats the difference when I assigned to a local reference
**/
ClassA *classaObject = [[ClassA alloc] init];
NSArray *tempArray = [classaObject classA_Method_1];
[classaObject release];
///here processing the tempArray to get a desired value from the array
NSString *str1 = [tempArray objectAtIndex:0];


Question ?
in this case3, I must release the tempArray right ?



case 4
/**
the following is the code from UIViewController sub class
whats the difference when I assigned to a local reference
**/
ClassA *classaObject = [[ClassA alloc] init];
NSArray *tempArray = [classaObject classA_Method_2];
[classaObject release];
///here processing the tempArray to get a desired value from the array

Question ?
in this case4, do I ned to do anything? copy or retain or release anything ? please telll

case 5
/**
the following is the code from UIViewController sub class
whats the difference when I assigned to a local reference
**/
ClassA *classaObject = [[ClassA alloc] init];
NSArray *tempArray = [classaObject classA_Method_1];
[classaObject release];

//here I am going to set the tempArray to another classes object like:

ChildController *a = [[ChildController alloc] init];
a.tableValues= tempArray
//push the child screen here
[a release];

question ?
I hope that this case5 is the same as case 1.. right ?