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

  • Home
  • SEARCH
  • 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 6178521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:28:16+00:00 2026-05-24T00:28:16+00:00

When my custom initializer fails, I am supposed to return nil. What is the

  • 0

When my custom initializer fails, I am supposed to return nil. What is the convention for cleaning up any memory I’ve allocated in my initializer, that I was expecting would be cleaned up in dealloc?

Here is a contrived example:

- (id)init
{
    if ((self = [super init])) {
        instanceVar1 = [[NSString alloc] initWithString:@"blah"];
        if (bad_thing_oh_noes) {
            return nil;
        }
    }
    return self;
}

- (void)dealloc
{
    [instanceVar1 release];

    [super dealloc];
}

A more realistic circumstance where I can’t efficiently check every error condition before I do allocations would be deserializing a complex object containing arrays and the like.

Anyway, do I clean up the allocated memory before returning nil, do I send a dealloc message to self before returning nil, or is all of this managed for me magically?

  • 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-05-24T00:28:17+00:00Added an answer on May 24, 2026 at 12:28 am

    If an error occurs during an initializer, you should call release on self and return nil.

    if (bad_thing_oh_noes) {
        [self release];
        return nil;
    }
    

    Also, you must make sure that it is safe to call dealloc on a partially initialized object.

    You should call release only at the point of failure. If you get nil back from the superclass’s initializer, you should not call release.

    Normally, you should not throw an exception upon initialization failure.

    An example from Handling Initialization Failure:

    - (id)initWithURL:(NSURL *)aURL error:(NSError **)errorPtr {
    
        self = [super init];
        if (self) {
    
            NSData *data = [[NSData alloc] initWithContentsOfURL:aURL
                                           options:NSUncachedRead error:errorPtr];
    
            if (data == nil) {
                // In this case the error object is created in the NSData initializer
                [self release];
                return nil;
            }
            // implementation continues...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I make custom default filter in interactive report? That filter is loaded
Hey! I have a xib file were i would like a round rect button
I got my own custom font working in Java, but I have one problem,
I've created a custom message type for use in resizing my Window , called
I have set up some custom symfony project-level settings in the file config/project.yml following
I have a .NET window Service which extend a custom base class, this serves
I've created a WCF DataService and for this service I require some custom authentication
Hi i am newbie in iphone and following beginning with iphone i am stuck
I've been trying to get an app hosted on EngineYard, and have run into
I have developed a WCF Service Application hosted in IIS 7.5 targeting .NET 3.5

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.