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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:56:07+00:00 2026-05-20T16:56:07+00:00

Is it better to set my retained member vars to nil or to release

  • 0

Is it better to set my retained member vars to nil or to release them when I am cleaning up? Setting a retained var to nil seems a safer way to release an object without risking a double release call on it.

Update: Let me elaborate that I’m referring to member vars that have been set to have the retain property i.e.:

@property (nonatomic, retain) SomeClass* mInstanceVar;
  • 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-20T16:56:07+00:00Added an answer on May 20, 2026 at 4:56 pm

    It’s best practice to release your instance variables first, and then set them to nil in your -dealloc method. I personally like doing that like so:

    [myVar release], myVar = nil;
    

    If you set your instance variables to nil, you’re not releasing them, and you’re causing a memory leak. Setting them to nil after releasing though will ensure that you’re not causing leaks, and if, for some reason, you try to access those instance variables later, you’re not going to get garbage memory.


    If you have an instance variable set up as such,

    @property (retain) NSObject *myVar;
    

    then it is not a good idea to call self.myVar = nil; during deallocation. If you have objects that have registered for KVO notifications on your instance variable, calling self.myVar = nil will send those notifications, and other objects will be notified, which is bad because they will be expecting you to still be in a valid state—you’re not if you’re in the deallocation process.

    Even if they’re not registered for KVO notifications, it’s still not a good idea to do that because you should never call methods that could rely on your object’s state when its state is inconsistent (some variables might/will be nonexistent), and you should simply handle the process yourself. [myVar release], myVar = nil; will suffice.

    If you want more information, read Dave DeLong’s answer to this question.


    For initializing, it is also not good to call property setters and getters (for much the same reason as above). In an -init call, you would set up the aforementioned variable as such:

    myVar = nil; // If you want to set it up as nil.
    OR
    myVar = [[NSObject alloc] init]; // Or set it up as an actual object.
    

    Avoid self.myVar = nil and self.myVar = [[NSObject alloc] init in cases where your class is in an undeterminate state (these calls are fine in -viewDidLoad and -awakeFromNib, though, because by that point, your class has been completely initialized, and you can rely on the instance variables to be in a complete state).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a better way of appending a set to another set than iterating
which one of the following is better? <c:set var=var1 value=false scope=request/> <c:if test=${someCondition}> <c:set
Which constitutes better object oriented design? Class User { id {get;set} } Class Office
Having the hours and minutes, is there any easier or better way to set
This issue is better demonstrated than explained, so I've set up a git repo
Suppose that you have to set 12 cookies, would it be better to store
Better way to do it (without QImage )?: QImage image(width, height, QImage::Format_RGB888); memcpy(image.bits(), m_frameRGB->data[0],
any better way to write this ? $(this).parent().parent().find( dd ul).toggle(); update.. I am trying
I saw a comment saying It'd be better to set the title property with
I couldn't set a better title for the question but my problem is this:

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.