views:

132

answers:

4

Actually I would say: yes, the Getter is the owner. So anyone who calls the Getter is not responsible for freeing the memory. Or more precisely, the object itself is the owner, but the Getter acts as a delivery mechanism of the instance variable to others. Is that right, or did I get that wrong?

A: 

Essentially you are correct, but its just a matter of convention. As long as your convention is applied consistently, and everyone working on the project agrees to that, then you're golden.

Where you may run into trouble is if a caller caches a pointer to some memory it gets from an accessor and then the object is destructed and now the pointer is dangling.

jeffamaphone
This is a reasonable answer in general, but it doesn't apply to Objective-C objects, which use reference-counting.
Jens Alfke
@Jens Alfke, a small correction, Foundation objects use reference counting, not all Objective-C objects. The Objective-C Object implemented by the basic runtime does not offer anything of the sort. It's only ivar is the isa.
dreamlax
A: 

It depends.

You should decide on object ownership policies. But in general, if a object is a sub-object (a member) of another, the object it's part of is responsible for its lifetime, and (generally) clean it up it its destructor.

Handing out memory to be cleaned-up by the caller is generally frowned on. In C, libraries that must allocate memory for callers (say, X Windows libs, or a C API for a database client) will hand out an opaque pointer, which must be destroyed by the caller by calling another library function:

 Window foo = APIcreateWindow();
 APIsetupWindow(foo, x, y, z );
 AOIshowWindow(foo);
 APIdestroyWindow(foo);  //forgetting to call this leaks memory

In that (hypothetical) code, Window is actually a typedef for a pointer to struct; APICreateWindow allocated memory for the struct, APIdestroyWindow calls free on it, after doing other API book-keeping.

In C++, we'd probably hand back a smart pointer, that destroys itself when nothing else refers to it.

tpdi
Cocoa is entirely different. Modern Objective-C runtimes have garbage collection but older or less sophisticated runtimes generally use reference counting. Returning a retained object from a method is usually not required because the caller can retain it themselves. Usually the only methods that return retained objects are methods that allocate them. For methods that allocate space there is the -autorelease method that can be used to add an object to an autorelease pool. The object's lifetime is guaranteed for the life of the caller's method but can be retained manually if needed.
dreamlax
+1  A: 

Since you are asking about Cocoa, which uses a reference-counting system, there is no such thing as "the" owner. Everyone with a retaining reference is a partial owner, responsible for releasing their reference. When the last reference is released, the object is deallocated.

It is the case that by convention getters do not give the caller a retaining reference, but that doesn't mean that a caller couldn't choose to explicitly retain an object.

smorgan
+5  A: 

It makes more sense to talk about objects, not methods, owning objects.

Quite often, the object that the getter is being called on is the owner of the object being returned, because the return value is an instance variable:

  • (Foo*) foo { return _foo; // my instance variable }

However, it's also common for a getter to create a temporary object that's been autoreleased, and return it:

  • (NSString*) sizeStr { return [NSString stringWithFormat: @"%d", self.size]; }

In this case, that object (the NSString) doesn't really have an owner, except perhaps the current autorelease pool, which will release it when it exits.

The important rules to remember in (non-GC) Cocoa programming don't have to do with ownership per se, but with when you have a reference to an object (that you'll have to release at some point) and when you don't.

Jens Alfke