views:

87

answers:

1

Case 1:

-(id)getAnObject{

        Object *someObject = [[Object alloc] init];
       //doing something

        return someObject;

}

Case 2:

-(void)dealWithAnObject{
            Object *someObject = [[Object alloc] init];
            [assignTheObjectToOther someObject];

}

Both Case 1 and Case 2 have some warning in XCode, what should I do to deal with these two? thank you.

+5  A: 

The golden rule of memory management is: everything needs to know which objects it owns. And only you can make that decision.

I highly recommend reading Apple's memory management guide. At least twice.

For your specific case:

  1. getAnObject doesn't ever release the object it creates. If you're going to return it, you'll want to autorelease it first.

  2. dealWithAnObject also doesn't release its object. You can either autorelease it or release it after the function call that uses it. Make sure that anything that uses it follows the same rules, and you'll be OK.

Jesse Beder