Memory management in ObjC/iPhone
- by Manu
Hi,
I have question in memory management (objective C). There are two ideal scenario.
============================= scenario 1 ========================================
(void) funcA
{
MyObj *c = [otherObj getMyObject];
[c release];
}
-(MyObj *) getMyObject //(this method is available in other OtherObj.m file)
{
MyObj *temp = [[MyObj alloc] init];
// do smothing here
return temp;
}
============================= scenario 2 ========================================
(void) funcA
{
MyObj *c = [otherObj getMyObject];
}
-(MyObj *) getMyObject //(this method is available in other OtherObj.m file)
{
MyObj *temp = [[myObj alloc] init];
// do smothing here
return [temp autorelease];
}
myObj is holding huge chunk of data.
In first scenario I am getting myObj(allocated) from other file so I have to release in my own method. (as per any C/C++ language library ,like strdup will return string duplicate which will realase later by developer not by strdup method).
In second scenario I am getting myObj(allocated) from otherObj.m file so otherObj.m file is responsible to release that allocated memory(mean autorelease)? Is it right?
Please let me know Which scenario is more efficient and valid as per apple memory guidelines.
Please Please don't show me any document link.
Thanks
Manu