Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8839255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:11:55+00:00 2026-06-14T10:11:55+00:00

As follow-up of sorts to Is returning nil from a [[class alloc] init] considered

  • 0

As follow-up of sorts to Is returning nil from a [[class alloc] init] considered good practice?, there’s a case that I haven’t seen any discussed much: what to do with an init that fails some preconditions before it can call the next init?

Example, suppose in this initWithStuff: method being passed nil or in general having no value to pass to initWithValue: is an absolute failure and we definitely want to return nil.

- (id)initWithStuff:(Stuff *)inStuff {
  if (!inStuff || ![inStuff hasValidValue])
  {
    // can't proceed to call initWithValue: because we have no value
    // so do what?
    return nil;
  }
  NSInteger value = [inStuff integerValue];
  return [super initWithValue:value];
}

Perhaps a clearer example is if the designated initializer method we wrap takes an object pointer and throws an exception if its passed nil. We definitely need to short-circuit that init call that would cause an exception.

My guess: init by any means possible, and only then release self before returning nil. If necessary, call bare init or any other initializer that will work to finish putting self into a known state before releasing it.

  // can't proceed to call super's initWithValue: because we have no value
  // so do what? do this:
  self = [super init]; // or initWithValue:0
  [self release];
  return nil;

And if there were no such initializer that will work without valid data, I guess one would need to construct some valid, dummy data. Or complain to its author and until then just return nil and live with the leak :^)

Also, how does ARC affect the situation?

My guess: still finish init by any means possible, then just return nil. You’d think setting self might be redundant, but in some cases it’s not. In any case, it but it needs to be there to silence a compiler warning.

  // can't proceed to call super's initWithValue: because we have no value
  // so do what? do this:
  self = [super init]; // finish init so ARC can release it having no strong references
  return nil;

Are my guesses wrong in any way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:11:57+00:00Added an answer on June 14, 2026 at 10:11 am

    Ideally, if a precondition fails, you don’t call [super init…]. You just release self (if not using ARC) and return nil:

    - (id)initWithStuff:(Stuff *)stuff {
        if (!stuff || ![stuff isValid]) {
            [self release]; // if not using ARC
            return nil;
        }
    
        if (self = [super init]) {
            // initialization here
        }
        return self;
    }
    

    The release takes care of deallocating self under MRC. Under ARC, the compiler will insert the release for you.

    However, there is a potential problem with this approach. When you release self (or when ARC releases it for you), the system will send the dealloc message to the object. And your dealloc method will call [super dealloc]. You could suppress the [super dealloc] under MRC, but you can’t avoid it with ARC.

    So the danger is that your superclass might assume that one of its instance variables has been initialized, and rely on that initialized value in its dealloc. For example, suppose this is the superclass:

    @interface SomeSuperclass : NSObject
    @end
    
    @implementation SomeSuperclass {
        CFMutableBagRef bag;
    }
    
    - (id)init {
        if (self = [super init]) {
            bag = CFBagCreateMutable(NULL, 0, &kCFTypeBagCallBacks);
        }
        return self;
    }
    
    - (void)dealloc {
        CFRelease(bag);
    }
    
    @end
    

    The problem here is that CFRelease requires its argument to not be nil. So this will crash during deallocation if you don’t call [super init] in your subclass.

    Given this problem, I have to change my initial recommendation. If you know that your superclass’s dealloc doesn’t have this sort of problem (because, for example, it checks pointers before dereferencing them or passing them to CFRelease), then you can safely not call [super init].

    If you don’t know that your superclass’s dealloc is safe, then my recommendation is that you move your preconditions out of init and into a class factory method.

    In other words, don’t treat alloc/init as part of your class’s public interface. Provide a class method for creating instances:

    // The class factory method.  Declare this in your header file.  This is how you
    // or any user of this class should create instances.
    + (id)myObjectWithStuff:(Stuff *)stuff {
        if (!stuff || ![stuff isValid])
            return nil;
    
        // self here is the class object, so it's appropriate to send `alloc` to it.
        // You don't want to hardcode the class name here because that would break
        // subclassing.
        return [[self alloc] initWithStuff:stuff];
    }
    
    // This is now considered a private method.  You should not declare it in your
    // header file, though in Objective-C you can't prevent the user from calling it
    // if he's determined to.
    - (id)initWithStuff:(Stuff *)stuff {
        // Precondition was already checked in myObjectWithStuff:.
        if (self = [super init]) {
            // initialization here...
        }
        return self;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Follow up question from Multi-core usage, threads, thread-pools . Are threads moved from one
Follow up from: Directdraw: Rotate video stream I managed to preview the camera's video
Follow up to this question for Facebook Friends.getAppUsers using Graph API that pulls friends
My question is sort of a follow on from this question below but I
How can I sort the elements of the list A so that they follow
When I write SQL queries, I find myself often thinking that there's no way
I'm implementing a script that adds images to a page from a folder. The
Basic question: what design principles should one follow when choosing between using a class
This is follow on from this question: Format List<T> to concatenate fields . The
A follow up Q from: CUDA: Calling a __device__ function from a kernel I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.