1

When I call readDataOfLength: on an NSFileHandle instance, do I need to release the returned NSData? Currently I'm not, but I would like to get rid of this nagging doubt.

SSteve
  • 10,550
  • 5
  • 46
  • 72

2 Answers2

5

Nope. Cocoa's memory management rules say that you only own (and thus need to release) objects returned from a method whose name contains one of "new", "alloc", "retain", or "copy" (NARC). Since that method contains none of those, you know you don't need to release it.

Matt Wilding
  • 20,115
  • 3
  • 67
  • 95
1

The standard that is observed very consistently within Objective C libraries is that a method beginning with "alloc" or "copy" (and, I'm thinking, some other obscure verb) returns a retained object, while everything else returns an autoreleased object.

Any exceptions should be well-documented in the specs.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • The obscure word is “new”, which hardly anybody uses (this may change as people adopt ARC…), and the other one, not obscure at all, is `retain`. Note that just because a method doesn't return an owning reference doesn't mean it returns an autoreleased object. – Peter Hosey Aug 27 '11 at 04:02
  • If a method does not return an autoreleased object (and is not one of the noted exceptions) then it *must* be documented. (This goes for user-written methods as well as the Objective C library.) – Hot Licks Aug 27 '11 at 11:48
  • As stated, untrue. What I think you meant to say is that “If a method *returns an owning reference* (and does not follow the naming patterns), then it *must* be documented”. That is true. A method can return an object without returning an ownership or autoreleasing the object. – Peter Hosey Aug 27 '11 at 18:48