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 8406957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:15:23+00:00 2026-06-09T23:15:23+00:00

Assuming our -init method only invokes messages on self , why is it common

  • 0

Assuming our -init method only invokes messages on self, why is it common to check if self != nil if messaging nil has no effect?

Let’s say we have an initializer as follows:

- (id)init
{
    self = [super init];
    if (self) {
        [self doThis];
        [self setFoo:@"Bar"];
    }

    return self;
}

Instead of checking self, we could write:

- (id)init
{
    self = [super init];
    [self doThis];
    [self setFoo:@"Bar"];

    return self;
}

Now if for some reason [super init] returns nil, there would be no difference in the outcome of the method as far as I know. Why then do we constantly perform this check?

  • 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-09T23:15:24+00:00Added an answer on June 9, 2026 at 11:15 pm

    You can send a message to nil, but you cannot access the instance variables of nil. You’ll get an EXC_BAD_ACCESS exception.

    Consider a class that has instance variables:

    @implementation MyObject {
        int instanceVariable;
    }
    
    - (id)init {
        self = [super init];
        instanceVariable = 7;
        return self;
    }
    

    What happens if [super init] returns nil in this example? You will try to access that instanceVariable off of a null pointer and you will get an exception.

    Even if you’re not accessing any instance variables, other things can certainly go wrong if you don’t check for self == nil.  You can easily leak malloc-allocated memory or file handles, or pass yourself to some method that’s not expecting nil.

    Other answers claim that you can leak objects if you don’t check for nil. For example:

    @implementation MyObject
    
    @synthesize someProperty; // assume it's an NSObject *
    
    - (id)init {
        self = [super init];
        [self setSomeProperty:[[NSObject alloc] init]];
        return self;
    }
    

    This won’t leak under ARC, even if self is nil. Under manual reference counting (MRC), this example will leak whether self is nil or not, because there’s nothing to balance the +1 retain count from [NSObject alloc].

    The proper way to do it under MRC is this:

    - (id)init {
        self = [super init];
        [self setSomeProperty:[[[NSObject alloc] init] autorelease]];
    }
    

    or this:

    - (id)init {
        self = [super init];
        NSObject *object = [[NSObject alloc] init];
        [self setSomeProperty:object];
        [object release];
        return self;
    }
    

    Neither of those will leak, whether self is nil or not.

    If you bypass the setter method, like this, you’ll just crash if self is nil:

    - (id)init {
        self = [super init];
        _someProperty = [[NSObject alloc] init];
        return self;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming that we only have one consumer and our redelivery policy will allow the
Our application has a table which stores all transactions records of clients in one
Our web app currently under development has authentication on all the pages. We can
I've built a CMS for our company which has a huge number of functions,
Our web-designer just finished up our site, and it has an HTML Form with
We have an installer for our application that has the file dotnetfx.exe when the
So assuming that in our work environment we've decided that the One True Faith
Recently our company has started measuring the cyclomatic complexity (CC) of the functions in
Am I correct in assuming that the only difference between "windows files" and "unix
Assuming I have a numerical string: var foo = 0; Assume I want to

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.