Skip navigation.
 
mlRe: Accessors Question
FROM : Jeff LaMarche
DATE : Thu Jun 12 21:20:58 2008

Out of curiosity, does anyone know what the synthesized accessors look 
like when you specify retain? Are they
The safe or unsafe ones?

Sent from my iPhone

On Jun 12, 2008, at 12:02 PM, Tito Ciuro <<email_removed>> wrote:

> Hello,
>
> Although I prefer the safer accessors, there has been one case where 
> I had no choice but to return the main object pointer:
>
> - (NSString *)foo
> {
>    return foo;
> }
>
> In my app I had a method that was populating a custom cell with a 
> few elements. Depending on the data source, I had to construct the 
> strings in different ways. Since this string had to be constructed 
> with up to 5 elements, when the table view had many rows, 
> performance suffered greatly during sort and memory consumption 
> spiked tremendously due to a barrage of newly allocated objects.
>
> I ended up writing something like:
>
> // Private version used only for performance
> - (NSString *)_foo
> {
>    return foo;
> }
>
> // Safer version used in all other cases
> - (NSString *)foo
> {
>    return [[foo retain] autorelease];
> }
>
> By using '_foo' during sorting I increased speed noticeably, while 
> reducing the memory footprint.
>
> -- Tito
>
> On 12 Jun 2008, at 11:27 AM, Jens Alfke wrote:
>

>>
>> On 12 Jun '08, at 10:41 AM, Andy Lee wrote:
>>

>>> I hadn't thought of this case.
>>> Thanks for the pointer to the docs -- I now see there is a 
>>> vulnerability in the accessors I've been writing.

>>
>> This is kind of a religious issue. Some people like the safer 
>> accessors. Some people see them as a very expensive* workaround for 
>> a problem that rarely occurs.
>>
>> I'm firmly in the latter camp. I've never used this 'safe' form of 
>> accessor, and have only rarely run into the kind of crash it 
>> prevents; and it was always pretty easy to track down and fix. (The 
>> fix is just for the caller to retain the value it got from the 
>> accessor, then release it when it's done using it.)
>>
>> Another alternative is to leave the getters simple, but change the 
>> _setter_ methods to autorelease the old value instead of releasing 
>> it; that prevents this same crash, but is less expensive because 
>> setters are much more rarely called than getters (and -autorelease 
>> isn't much more expensive than -release.)
>>
>> \Jens
>>
>> * A basic accessor requires one or two machine instructions to do 
>> the actual work; whereas -retain and -autorelease involve extra 
>> method dispatches that each acquire a global lock and do a 
>> hashtable lookup. Obviously any one call isn't going to take a 
>> noticeable amount of time, but accessor calls are so damn 
>> ubiquitous that this can have an overall impact on app performance 
>> in some cases. Not to mention memory usage, since autoreleased 
>> objects have a longer lifespan and can build up during 
>> loops._______________________________________________
>>
>> Cocoa-dev mailing list (<email_removed>)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>> http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
>>
>> This email sent to <email_removed>

>
> _______________________________________________
>
> Cocoa-dev mailing list (<email_removed>)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
>
> This email sent to <email_removed>

Related mailsAuthorDate
mlAccessors Question Jeff LaMarche Jun 12, 19:21
mlRe: Accessors Question Jean-Daniel Dupas Jun 12, 19:31
mlRe: Accessors Question Andy Lee Jun 12, 19:33
mlRe: Accessors Question Bill Bumgarner Jun 12, 19:35
mlRe: Accessors Question Andy Lee Jun 12, 19:41
mlRe: Accessors Question Jens Alfke Jun 12, 20:27
mlRe: Accessors Question Tito Ciuro Jun 12, 21:02
mlRe: Accessors Question Jeff LaMarche Jun 12, 21:20
mlRe: Accessors Question mmalc crawford Jun 12, 22:17