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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:30:56+00:00 2026-05-12T12:30:56+00:00

So I modified Apple’s PageControl example to dynamically load various navigation controllers (along with

  • 0

So I modified Apple’s PageControl example to dynamically load various navigation controllers (along with their root view controllers) into the scroll view. I also added a technique that attempts to unload a navigation controller when it’s no longer needed. I’ve only been at ObjC for a little over a month, so I’m not sure if I’m doing the unloading correctly. Please see my code below, followed by my questions.


First I create a mutable array and fill it with nulls, just like Apple does:

// Create dummy array for viewControllers array, fill it with nulls, and assign to viewControllers
NSMutableArray *array = [[NSMutableArray alloc] init];
for (unsigned i = 0; i <= kNumberOfPages; i++)
{
    [array addObject:[NSNull null]];
}

self.viewControllers = array;
[array release];

…Later, I fill the array with UINavigationController objects like so (this is just partial code, please excuse the missing parts…the main idea is that I alloc a couple of things, assign them and then release):

id controller = [[classForViewController alloc] initWithNibName:NSStringFromClass(classForViewController) bundle:nil];
    navController = [[UINavigationController alloc] initWithRootViewController:controller];
    [controller release];
    [self.viewControllers replaceObjectAtIndex:page withObject:navController];
    [navController release];

…Finally, if a page doesn’t need to be loaded anymore I do this:

[self.viewControllers replaceObjectAtIndex:i withObject:[NSNull null]];

Questions:

  1. My understanding is that once I replace the navigation controller in my viewControllers array with null, the array releases the navigation controller. Thus the navigation controller’s retain count hits zero and it no longer takes up memory. Is this correct?

  2. What about the root view controller inside the navigation controller? Do I need to do anything with it or does it get released automatically once the navigation controller’s retain count hit zero?

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-05-12T12:30:57+00:00Added an answer on May 12, 2026 at 12:30 pm
    1. Yes. Any object put into a collection is sent a retain message. Likewise any object removed from a collection is sent a release message, the cause of the removal is irrelevant.
    2. Yes, all objects will release all the objects it owns when they are released.

    This all boils down to the simple principle of ownership that Cocoa defines:

    • You own the object if you received it as return value by calling a method that:
      1. Is named alloc or new.
      2. Contains the word copy, such as copy and mutableCopy.
    • You own the object if you call retain.
    • You may only call release and autorelease on objects you own.
    • You must release all owned objects in your dealloc methods.

    There is just one exception; delegates are never owned. This is to avoid circular references and the memory leaks they cause.

    As a side effect this also means that when you yourself are implementing a method, you must return an auto released object unless you are implementing new, or a method with copy in it’s name. Objects returned as out arguments are always autoreleased.

    Follow this strictly and Objective-C can be treated as if it is garbage collected 95% of the time.

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

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think that could be due to the fonts setting… May 12, 2026 at 12:31 pm
  • Editorial Team
    Editorial Team added an answer We use git for all of our source code. It… May 12, 2026 at 12:31 pm
  • Editorial Team
    Editorial Team added an answer Vim needs a backslash for +, so try :%s/^\d\s\+// May 12, 2026 at 12:31 pm

Related Questions

I have an application that opens a new window on clicking a link. This
I am relatively new to game development so I decided I wanted to create
I want to use git to allow me to work on several features in
I'm using git-svn and there are IDE project files in the svn repository. Everytime
I want to generate a summary of the files that are in one tree

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.