0

Possible Duplicate:
memory management objective c - returning objects from methods

I have a confusion with retaining object. If I have an function that will return an object like this:

- (object) functionA {

   NSObject* o = [[object alloc] init];

   return o;
}

The object returned will have retain count of 1.... But is this best practice to do this or shall i put autorelease first and let the caller retain the object.

Community
  • 1
  • 1
LittleFunny
  • 8,155
  • 15
  • 87
  • 198
  • Please format your code next time. – Carl Norum Mar 19 '12 at 22:58
  • See http://stackoverflow.com/questions/710288/where-are-the-best-explanations-of-memory-management-for-iphone and the [official Memory Management rules](http://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/MemoryManagement.html). – jscs Mar 19 '12 at 23:02

1 Answers1

3

In that case you'd return an autoreleased object. The NARC rule states that any method that does not include the words 'new', 'alloc', 'retain' or 'copy' should return a non-owning reference. The method functionA includes none of those words in its name.

Tommy
  • 99,986
  • 12
  • 185
  • 204