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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:12:34+00:00 2026-06-12T06:12:34+00:00

I have a rather unusual situation, and I’m not quite understanding the memory management

  • 0

I have a rather unusual situation, and I’m not quite understanding the memory management properly.

I have a UITableViewController for displaying messages that then creates a UINavigationController and adds its view as a subview of the current view in order to display it. The issue I’m having is that Xcode reports I have a potential memory leak (and I agree) due to not releasing the UINavigationController, but when I release it as in the code below, the app crashes when I click back to return to the Table View.

I used a retained property in the UITableViewController to keep track of the current UINavigationController and manage the retain counts, but clearly I’m missing something here.

Note: The crash occurs when the Back button is clicked with message -[UILayoutContainerView removeFromSuperview:]: unrecognized selector sent to instance 0x5537db0

Also note that if I remove the [nc release] line of code, it works just fine.

Here’s my code:

@property(nonatomic, retain) UINavigationController *currentNavigationController;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UINavigationController *nc = [[UINavigationController alloc] init];

    CGRect ncFrame = CGRectMake(0.0, 0.0, [[self view] frame].size.width, [[self view] frame].size.height);
    [[nc view] setFrame:ncFrame];

    // I created a CurrentNavigationController property to 
    // manage the retain counts for me
    [self setCurrentNavigationController:nc]; 

    [[self view] addSubview:[nc view]];
    [nc pushViewController:messageDetailViewController animated:YES];


    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonSystemItemRewind target:[nc view] action:@selector(removeFromSuperview:)];


    nc.navigationBar.topItem.leftBarButtonItem = bbi;
    [bbi release];

    [nc release];
}
  • 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-12T06:12:35+00:00Added an answer on June 12, 2026 at 6:12 am

    UINavigationController “nc” that you created is only usable inside this method. It is not stored anywhere after this method (because you release it). So you add navigationController’s view to your class view as subview, and then you delete navigationController. That’s wrong. When views and viewControllers try to refer to their navigationController(when it does not exist), your app will crash.

    Here is code for didSelectRowForIndexPath method:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
    
        UINavigationController *nc = [[UINavigationController alloc] init];
    
        CGRect ncFrame = CGRectMake(0.0, 0.0, [[self view] frame].size.width, [[self view] frame].size.height);
        [[nc view] setFrame:ncFrame];
    
        [self setCurrentNavigationController:nc]; 
        [nc release];
    
        [[self view] addSubview:[self.currentNavigationController view]];
    
        UIViewController *viewCont = [[UIViewController alloc] init];
        [viewCont.view setBackgroundColor:[UIColor greenColor]];
    
        [nc pushViewController:viewCont animated:YES];
    
        NSLog(@"CLASS %@",[[self.currentNavigationController view]class]);
    
        UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonSystemItemRewind target:[self.currentNavigationController view] action:@selector(removeFromSuperview)];
    
    
        self.currentNavigationController.navigationBar.topItem.leftBarButtonItem = bbi;
        [bbi release];
    }
    

    Selector for removeFromSuperview method shouldn’t have “:” in the end. It has no arguments 🙂

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

Sidebar

Related Questions

I have a rather unusual layout that I'm trying to make a reality. There
I am faced with rather unusual situation I will have url in any of
I have a rather complex situation that I need to get working, for some
I have rather large project that uses ICU regex classes. Basically it might run
I have some rather odd behavior in my D program that I've narrowed down
I have a rather enormous project in which I'm trying to retrofit in-memory data.
I have a rather huge query that is needed in several stored procedures, and
I have a rather unusual problem, and it is hurting my brain. Problem: Given
I accidentally deleted some builds for a job that I would have rather kept.
We have rather Big machine 100GB+ memory and 8+ cores in it. Server wide

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.