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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:01:27+00:00 2026-05-23T23:01:27+00:00

I have a pretty basic question. In some examples I’ve seen, objects are just

  • 0

I have a pretty basic question. In some examples I’ve seen, objects are just released in the dealloc method. In others, the objects are released and then set to nil. Is there a reason for this? Is setting to nil after releasing advantageous?

  • 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-23T23:01:28+00:00Added an answer on May 23, 2026 at 11:01 pm

    Three ways to dealloc

    1. Just release

    - (void)dealloc {
        [airplane release];
        [super dealloc];
    }
    

    Now the object reference points to a random position, which may be one of two things:

    1. Most likely it is garbage, because the memory position can’t be interpreted as an object.
    2. Rarely it will be a different object, because memory have been reused to create a new object.

    The effect of a further method calls through this pointer is one of these three (which one is undefined):

    • A crash with EXC_BAD_ACCESS because the pointer points to garbage.
    • A crash with undefined selector because it points to a valid object which doesn’t have that method.
    • A successful method execution because the new object has a method by the same name.

    2. Release and nil

    - (void)dealloc {
        [airplane release], airplane = nil;
        [super dealloc];
    }
    

    Now the object reference is nil and any further method calls are ignored. This may silently cause a defined but unforeseen lateral effect in your code, but at least it doesn’t crash your application.

    3. Nil and release

    - (void)dealloc {
        id temp = airplane;
        airplane = nil;
        [temp release];
        [super dealloc];
    }
    

    This is the same as before, but it removes that small window between release and nil where the object reference points to an invalid object.

    Which one is best?

    It is a matter of choice:

    • If you rather crash choose just release.
    • If you rather ignore the mistake choose nil+release or release+nil.
    • If you are using NSZombieEnabled=TRUE then just release, don’t nil the zombie!

    Macros and zombies

    A easy way to defer your choice is using a macro. Instead [airplane release] you write safeRelease(x) where safeRelease is the following macro that you add to your .pch target file:

    #ifdef DEBUG
      #define safeRelease(x) [x release]
    #else
      #define safeRelease(x) [x release], x=nil
    #endif
    

    This macro doesn’t respect zombies. Here is the problem: when NSZombieEnabled is TRUE the object turns into a NSZombie. If you nil its object reference, any call sent to him will be ignored.

    To fix that, here is a macro from Kevin Ballard that sets the pointer to an invalid made up reference ONLY when NSZombieEnabled is FALSE. This guarantees a crash during debug time if zombies are not enabled, but leaves the zombies be otherwise.

    #if DEBUG
      #define safeRelease(x) do { [x release]; if (!getenv("NSZombieEnabled")) x = (id)0xDEADBEEF; } while (0)
    #else
      #define safeRelease(x) [x release], x = nil
    #endif
    

    References

    Apple doesn’t have a recommendation on which one is best. If you want to read the thoughts of the community here are some links (the comment threads are great too):

    • Dealloc Jeff Lamarche
    • Don’t Coddle Your Code Daniel Jalkut
    • More on dealloc Jeff Lamarche
    • To nil, or not to nil, that is the question Ching-Lan Huang
    • Defensive Coding in Objective-C Uli Kusterer
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this should be a pretty basic question. I would like to have some html
Pretty basic programming question, I know PHP have a function for it, but does
This a pretty basic question. Let's say I have this iPhone/iPad app that, at
I'm just learning Objective-C and have a what I'm sure is a pretty basic
I'm new to Ruby, so this may be a pretty basic question. I have
This is a pretty basic question but I'm still unsure: If I have a
I have a pretty basic windows form app in .Net. All the code is
I have a pretty basic challenge-based iPhone game, and I wanted to know what
I have created a pretty basic Flash website for a client and am having
I have a user control that is pretty basic. It contains several TextBox controls,

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.