views:

372

answers:

3

I've just downloaded the Facebook iOS SDK and I noticed that in the sample code that comes with the SDK whenever it creates an instance of the Facebook class it does it like this:

_facebook = [[[[Facebook alloc] init] autorelease] retain];

where _facebook is a member variable of the calling object (i.e. not a local variable).

Can anyone explain exactly what's the point of autoreleasing and then retaining it?

+11  A: 

It effectively does nothing beyond consuming some cycles and memory.

Or, more precisely, in a correctly written application, it does nothing. In an incorrectly written application, it might mask a bug by lengthening the lifespan of the _facebook object. However, it isn't a real fix.

I found a similar line of code in http://github.com/facebook/facebook-ios-sdk/blob/master/sample/DemoApp/Classes/DemoAppViewController.m If that is what you are referring to then, yes, it is nonsense.

bbum
The alternative theory is that it's a symptom of memorization without understanding. “To create an object, I must write [[[ClassName alloc] init] autorelease]; to hold onto it; I must retain it.” Only the author of the statement knows for sure.
Peter Hosey
Running blame on that file attributes it to one dev, maybe someone should send an email asking for clarification on Github ;) http://github.com/facebook/facebook-ios-sdk/blame/master/sample/DemoApp/Classes/DemoAppViewController.m
Shazron
Yeah, that's what I thought... but being in the "official" Facebook SDK I thought maybe the original developer knew something I didn't, so I figured I'd ask. Thanks for your input.
filipe
"official" doesn't always mean "best".
pixel
Added a comment on the actual line on Github: http://github.com/facebook/facebook-ios-sdk/commit/8a1b923b39be8215857dd2b74e48e77a9e54bbaa#commitcomment-138182
Shazron
A: 

As bbum said, this is utter rubbish. I'm surprised code like that calls itself an SDK.

This code is just inefficient and confusing and is no different in final result to allocate/init.

Jasarien
A: 

Will get it fixed

yujuan
Cool. As far as buggy LoC's go, it is -- at least -- a no-op. If there was some motivation behind it, I'd like to know what it is so that a proper bug can be filed against the llvm static analyzer to identify the real problem automatically.
bbum
That's great, thanks yujuan. And let me take the opportunity to thank you for your work on the Facebook iOS SDK. Despite that minor confusion, it's really helpful. Thanks!
filipe