tags:

views:

56

answers:

2

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

+1  A: 

May I suggest the documentation on memory management for the iPhone?

chpwn
Here getter method is giving you one allocated object. Same like alloc method... but why we should send it to autorelease pool. We can distroy this after gettter method called. I am the responsible person to get the object so I am owner of this method... why apple is not allowed to release after getter method .. it suggest we have keep it in autorelease pool becasue gettter method is owner of this object... That's mean autorelease pool going to increase.. it will create problem if I am calling this method multiple time?
Manu
+2  A: 

The second approach is preferable. The convention is that only "alloc" and "copy" methods should return an object that is the caller's responsibility to release. This convention is for ease of maintenance and has nothing to do with efficiency.

Efficiency (of the memory kind) only really comes into play if you're considering calling getMyObject in a loop with many iterations. In that case, autoreleased MyObj objects will accrue in memory because they are not released until the end of the run loop iteration. If that's a problem, move the alloc/init outside of the method call so that you can release the object yourself at the end of each iteration of your loop.

Tom
If each object returned from getter method will goes to autorelease pool.. than autoreleas pool will increase and might be which can create memory shortage? is it possible?
Manu
Jonathan Sterling
@Manu: That's correct. That's why you might want to move `[[MyObj alloc] init]` out of `getMyObject` and into `funcA`. Then you can release the object in `funcA` like you do in "scenario 1".
Tom