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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:37:41+00:00 2026-05-31T15:37:41+00:00

I have created two view controllers in which there is UIImage animations.Its crashing frequently

  • 0

I have created two view controllers in which there is UIImage animations.Its crashing frequently and showing memory leaks in xcode instrument.

My Controllers Code-

- (void)viewDidLoad {
     NSArray *firstArray;
     firstArray = [NSArray arrayWithObjects:    
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"up0001" ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"up0002" ofType:@"png"]],
                                      ::
                                      ::
                   [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"up0035" ofType:@"png"]], nil];


     imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];

     imgView = UIViewContentModeScaleToFill;

     [imgView setAnimationImages:firstArray];

     imgView.animationDuration = 1.75;

     imgView.animationRepeatCount = 0;

     [imgView startAnimating];

     [self.view addSubview: imgView];
}
- (void)dealloc {
    [super dealloc];
    [imgView release]; 
    imgView = nil;
}

And I am changing viewcontrollers as my rootviewcontroller by getting appdelegate object and calling following appdelegate function in my Appdelegate.m(Please suggest any good approach)

- (void)changeRootViewController:(NSString *)controllerName
{

    if(self.viewController){
        [self.viewController.view removeFromSuperview];
        self.viewController=nil;
    }

    if (controllerName == @"ViewController") {
        ViewController *lviewController =[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        self.viewController =  (RootViewController *)lviewController;
        [lviewController release];
        lviewController.view = nil;
        [self.window setRootViewController:self.viewController]; //LEAKS 100%
    } else if (controllerName == @"MainViewController") {
        // Use a different VC as roowViewController
        MainViewController *lviewController =[[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
        self.viewController =  (RootViewController *)lviewController;
        [lviewController release];
        lviewController.view = nil;
        [self.window setRootViewController:self.viewController]; //LEAKS 100%
    } else if (controllerName == @"SecondViewController") {
        // Use a different VC as roowViewController
        SecondViewController *lviewController =[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
        self.viewController =  (RootViewController *)lviewController;
        [lviewController release];
        lviewController.view = nil;
        [self.window setRootViewController:self.viewController];  //LEAKS 100%
    }

    [self.window makeKeyAndVisible];
}

And calling this in my respective controlers button pressed as –

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate changeRootViewController:@"ViewController"];

OR

 AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate changeRootViewController:@"MainViewController"];

I want to manage view controllers from my main controller swapping controllers in and out without Navigationbar.Please help me figure out the best possible approach and avoid leaks as well.

  • 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-31T15:37:42+00:00Added an answer on May 31, 2026 at 3:37 pm

    Look at this piece of code:

    
    if (controllerName == @"ViewController") {
            ViewController *lviewController =[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
            self.viewController =  (RootViewController *)lviewController;
            [lviewController release];
            lviewController.view = nil;
            [self.window setRootViewController:self.viewController]; //LEAKS 100%
        }
    

    When you alloc “lviewController”, its retain count is 1;
    when you do “self.viewController” as I suppose viewController is a retained property, then lviewController retain count is increased to 2;
    then you release it, balancing the previous alloc, and retain count returns to 1;
    finally you assign it to rootViewController, which is a retained property, so again lviewController retain count is 2;
    finally when you “swap” view controller in your window, the lviewController is released, so it retain count becomes 1. As you can see, it will never be dealloc’d. This means that each time you call this function, you make a leak.

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

Sidebar

Related Questions

I have two controllers Project and Tag, both of which have a create view
I have created an alert view with two buttons using the following code: UIAlertView
I have a UITabBarController with four view controllers. Two of those need to be
I have created two viewController classes such that one is superclass of another.i have
I have created two imageViews promatically as shown below: public void createImageViews(Integer count){ ImageView[]
I have created two WindowSurface (WPF), and I want to navigate betwen them. I've
I have created two tables & inserted values as shown below . Table 1
I have created two Pages Page1.xaml and Page2.xaml in windows8 metro apps. I have
I have created two DbContexts, one is for application configuration, the second is for
I have created two folders layout and layout-land with two xml files, one for

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.