| |||
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] |
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.
What I'd like to add is this. Sharing a return value between caller and method ideally should be fail safe.
To be fail safe the returned value must be under ownership of the caller.
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_];
}
If the memory is solely owned by the method (instance), this means a weakening in the fail-safeness
- (NSString *) name
{
return( name_);
}
This transfers a pointer to memory, not owned by the caller.
If name_ gets set to something else as a side effect to another call to that instance, I can expect crashes, because the previously returned value might now point to invalid memory.
This method provides therefore only a limited kind of read access to the return value. That limited access says, you may access that value, if you know, that it hasn't changed in the mean time. (In 99% of all cases, I think you will be able to say, yes I can do that :), otherwise you'd have problems using NSMutableArrays)
The reason why you get these funny problems when name_ is mutable is, because you are not yielding access completely, the contents are still subject to change by the messaged instance. That's why using copy] autorelease] would be proper, as it also gives the full access rights of the return value contents to the caller (within the scope... bla bla).
I am not promoting the use of retain] autorelease] (I dislike slowness :)), but I don't mind it to be the documentation default.
| References: | |
| >Re: Memory Management (From: Nat! <email@hidden>) |
| 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.