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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:07:08+00:00 2026-06-01T07:07:08+00:00

i was reading this code, where setRegions is called after RootViewController is released :

  • 0

i was reading this code, where setRegions is called after RootViewController is released : i find it a bit strange : does it mean RootViewController is still accessible, even if it was released and self.navigationController “owns” it ?

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    // Create the navigation and view controllers
    RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
    UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    self.navigationController = aNavigationController;
    [aNavigationController release];
    [rootViewController release];

    [rootViewController setRegions:[Region knownRegions]];

    // Configure and display the window
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}

Thanks

  • 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-01T07:07:09+00:00Added an answer on June 1, 2026 at 7:07 am

    This is bad code.

    An object should retain another object for as long as it cares about it. And in this case that rule is broken. The rootViewController is released, and then as you note, a method is called on it. This can be dangerous.

    In this case, it works. This is because rootViewController is passed to another object, which retains it. So when we release it, it still has a positive retain count and is not deallocated. So our reference to it still works, and methods called on it work fine.

    But lets say some implementation changed and initWithRootViewController: now no longer retained it’s argument for some reason (an assumption you can’t really make all the time). Suddenly this all crashes because rootViewController gets deallocated.

    To fix this funk, you just need to move [rootViewController release]; to after the last useful reference of that object in this function. Your code then becomes more robust and more correct.

    - (void)applicationDidFinishLaunching:(UIApplication *)application {
    
        // Create the navigation and view controllers
        RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
        [rootViewController setRegions:[Region knownRegions]];
        UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
        self.navigationController = aNavigationController;
    
        // Release temporary objects since we've now sent them to other other objects
        // which may or may not retain them (we don't really care which here)
        [aNavigationController release];
        [rootViewController release];
    
        // Configure and display the window
        [window addSubview:[navigationController view]];
        [window makeKeyAndVisible];
    }
    

    Last thing to note: release and dealloc are very different things. release does not necessarily destroy objects. It simply decrements the retain count by one. And if that retain count ever gets to zero, only then is the object is deallocated. So this code works because a release happens but without triggering a dealloc.

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

Sidebar

Related Questions

I am reading On lisp and encountered this code (I simplified it a bit).
I'm reading this code sample : And since I don't know C#, I decided
Reading this question I found this as (note the quotation marks) code to solve
I'm reading this C++ open source code and I came to a constructor but
Well I was reading this post and then I came across a code which
I'm learning by reading this tutorial: Link Here's the code: <?php require_once 'Zend/Loader.php'; class
I was reading a blog where the writer said this Code doesn’t exist unless
I'm reading Programming Perl , and I found this code snippet: sub new {
I've tried everything from reading the Netbeans help to browsing Google. This code works
i'm starting to use modal dialog; i create this code reading, and with the

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.