| |||
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] |
In other environments the assignment would prevent result from being freed during the lifetime of the variable. Since it cannot be done here the "solution" is too extend a little bit the lifetime of objects so that it works a little bit more like a garbage collected environment.
This is thinking in another language, and forcing Foundation / Objective-C into that mindframe. It doesn't help putting "garbage collection" as a concept or a goal to be aspired to, into this mail thread.
Where on earth did you get that from?!? Reference counting is a form of garbage collection. It isn't a perfect form of garbage collection because it can't handle cycles, but it still is a form of garbage collection. Retain/release, of course, is not fully automatic garbage collection, because it is at best semi-automatic.
Anyway, all this is only necessary when the callee does not hang on to the object. If the callee does hang on to the object, there simply is no *need* to extend the life of the object.
[examples]
Autorelease pools have some clear semantics. Every object added to an autorelease pool gains a property, a guaranteed minimum lifetime. Not more not less. Autorelease therefore guarantees (if result is autoreleased) that your case 1 will work
It guarantees no such thing.
and also guarantees that case 2 has a high chance of failure.
Ditto.
What I'd like to add is this. Sharing a return value between caller and method ideally should be fail safe.
There are several levels to this statement.
At the most general level you are asking for referential transparency, something you are never ever going to get in an imperative language, and Objective-C is always going to remain an imperative language.
However, I think it is very good that you did bring it up in this generality, because I think that this is the (somewhat hidden) motivation behind some of the things that are being said. I find this motivation understandable, because referential transparency is a nice thing to have. However, the only way to actually get referential transparency is to have a language designed from the ground up to support it. It is one of these things that you absolutely cannot bolt on to a language after the fact. How this usually works is that there are *no* references whatsoever, only values. So there can never be aliasing of pointers/references because there are no pointers/references. Conceptually, everything is copied all the time. So if you insert an element into a collection, a new collection is created that has the element inserted at the right spot. The old collection is not modified. Nothing is ever modified.
For those int, double return values it's trivial, because the value is copied through registers or stack. If there is memory involved then there needs to be a clear protocol for the transfer or sharing of that memory.
It isn't really the memory that is a problem. It is references to values (vs. values) and the aliasing that can happen when you are allowed to have more than one reference to a value.
If the memory is owned by the caller, then there is no problem, the caller temporarily grants read/write access to its memory area, and can peruse it at will on return:
- (void) getName:(NSMutableString *) s
{
[s setString:name_];
}
Yes. However, I think you are forgetting that we are dealing with objects. Just as it is valid for one method to grant access to its 'memory area' to another method, it is just as valid for one *object* to grant access to its 'memory area' (instance variables) to another object.
this is ideal and fail safe. It is not done, because it is deemed to cumbersome. The caller must create temporary objects and release >> them.
It is not fail-safe, but it is cumbersome.
If the memory is solely owned by the method (instance), this means a weakening in the fail-safeness
I don't agree with this. It is sharing, just like your other case, except that it is the *object* doing the sharing, not the method.
- (NSString *) name
{
return( name_);
}
This transfers a pointer to memory, not owned by the caller.
In the first case, you also transfered a pointer to memory, from the caller to the callee. This is just as unsafe because the callee could have released the memory. There is no difference!
| Home | Archives | FAQ | Terms/Conditions | Contact | RSS | Lists | About |
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE
Contact Apple | Terms of Use | Privacy Policy
Copyright © 2007 Apple Inc. All rights reserved.