Re: Exiting cleanly the init method under ARC
Re: Exiting cleanly the init method under ARC
- Subject: Re: Exiting cleanly the init method under ARC
- From: Tito Ciuro <email@hidden>
- Date: Wed, 27 Jun 2012 10:01:11 -0700
Thanks Quincey!
On Jun 27, 2012, at 9:54 AM, Quincey Morris wrote:
> On Jun 27, 2012, at 09:12 , Tito Ciuro wrote:
>
>> However, if something critical happens within the init method when ARC is activated, what would be the proper way to exit? Would it look like this?:
>>
>> - (id)init
>> {
>> self = [super init];
>>
>> if (self) {
>> if (<somecondition != good>) {
>> // Cleanup here if needed...
>> return nil;
>> }
>> }
>>
>> return self;
>> }
>
> Yes, just return nil.
>
> Like other local variables, 'self' will be released automatically (if non-nil) at exit from its scope, which is the entire method in this case. Because this is an 'init'-family method, the return value is retained (if non-nil) on execution of the 'return' statement.
>
> If you put those 2 behaviors together, you'll see that your ARC code has the same memory management effect as your non-ARC code, no matter which 'return' statement is executed.
>
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden