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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:01:00+00:00 2026-05-24T13:01:00+00:00

Is this the correct (best?) way to release views retained in viewDidLoad , in

  • 0

Is this the correct (best?) way to release views retained in viewDidLoad, in iOS 4.x or lower? Is there anything else to consider?

- (void) viewDidUnload
{
    [super viewDidUnload];
    [self releaseViews];
}

- (void) dealloc {
    [self releaseViews];
    [super dealloc];
}

#define SAFE_RELEASE(a) [a release]; a = nil;

- (void) releaseViews {
    SAFE_RELEASE(myView1);
    SAFE_RELEASE(myView2);
    SAFE_RELEASE(myView3);
}
  • 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-24T13:01:01+00:00Added an answer on May 24, 2026 at 1:01 pm

    The -dealloc is correct and the -viewDidUnload will work, but typically retained views are only nilled in -viewDidUnload and not released. This seems to be Apple’s practice as well and it is what they’ve baked into the Xcode when you create an auto-generated IBOutlet via the Assistant editor.

    For auto-generated IBOutlets, the auto-generated -viewDidUnload looks like this:

    - (void)viewDidUnload {
        [self myView1:nil];
        [self myView2:nil];
        [self myView3:nil];
        [super viewDidUnload];
    }
    

    Also, from the Apple docs on -viewDidUnload:

    The preferred way to relinquish ownership of any object (including those in outlets) is to use the corresponding accessor method to set the value of the object to nil. However, if you do not have an accessor method for a given object, you may have to release the object explicitly

    So, there you go. If your outlet has a property associated with it (which they all should anymore), then nil it in -viewDidUnload — but don’t release it. This makes sense when you consider what is actually happening in a synthesized accessor; the code looks something like this:

    - (void) setMyView1 : (UIView *) view {
       if (myView1) // the associated IVAR is already set
          [myView1 release];
    
       myView1 = [view retain];
    }
    

    As you can see, setting a synthesize property to nil implicitly releases the retained object.

    Also from the docs in regards to -dealloc:

    If you implement this method but are building your application for iOS 2.x, your dealloc method should release each object but should also set the reference to that object to nil before calling super.

    Unless you are supporting iOS2.x, there is no need to set objects to nil in dealloc.

    So, to summarize Apple’s docs regarding -viewDidUnload and -dealloc:

    • In -viewDidUnload, nil properties (including IBOutlet properties), but don’t release them
    • In -dealloc release properties, but don’t nil them (unless building for 2.x).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this the correct/best SPARQL query to get the count of items in rdf:list:
What do you think? Is this correct or are there memory leaks? Source: #include
I'm confused with bridge and bridge_transfer , is this correct? -(void)getData{ ABAddressBookRef addressBook =
Is this the correct way to do it? <a id=load href=# class=btn load onclick=request(this);
Is this the correct (or even a valid way) to use emums in Objective-C?
Is this the correct way to obtain the most negative double in Java? double
This is a simple question: Is this a correct way to get an integer
Is this the correct way to pass two ids in jQuery? I am having
I'd like to know the correct / best way to handle concurrency with an
I'm wondering what's the best way -- or if there's a simple way with

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.