Reputation: 2439
I was reading through Apple's doc of Basic Memory Management Rules. I came across a sentence, which is "Any object may have one or more owners."
What does this mean? An object having 2 owners. I am not really familiar with the OOP concepts.
Upvotes: 0
Views: 110
Reputation: 39296
In memory management, if an object owns a reference to another object it retains it.
Multiple objects can retain an object and when there are no retains on the object, no one owns it and it goes away. A retain increases a reference count and a release decrements it.
A good analogy is adding a leash to a pet. Multiple folks can add a leash but if no one has a leash on it, it can go away :)
If you are going to use a reference to an object outside of the immediate function that you are getting the reference, then you should retain it. If you call alloc, copy, mutableCopy to get the reference then you just retained it. If you get it by another message name, the standard is it's autoreleased (which is fine) and will go away at some near point in the future outside the scope of that function.
Upvotes: 3
Reputation: 19323
By "owns" they mean "holds a reference to." iOS memory management is explicit, it is done by reference counting. The "retain" message sent to an object increases the reference count, and the "release" message decrements the reference count. When the reference count reaches 0, the object is freed (and is sent the "dealloc" message first). This applies to objects in the NSObject hierarchy.
So when one object is handed a reference to an object that it wants to continue to use at some time in the future, that object keeps a copy of the pointer to the object and sends it a "retain" message so that the object won't be freed while the "owner" wants to access it.
Upvotes: 2