memory not releasing with ARC and storyboard in iO

2019-02-19 03:06发布

问题:

i'm cracking my head on memory issues with my app, the app are working fine except that it will crash once it hit low memory warning and are very very very laggy when using it for 10 to 20 minutes.

EDIT: how to poptoviewcontroller?

introvideo-> welcomeview & tutorialview-> mainviewcontroller-> scannerviewcontoller-> questionview ->(if answer correct -> correctView) else ->wrongView

how do i pop back to mainView controller ?

the below code are to solve adding view controller to the navigationcontroller.viewcontroller stack. As i'm using storyboard pushing from viewcontroller to another view contoller with out poping. the code will pop to the viewcontroller that are already in the viewcontroller stack.

the flow of the of my storyboard as attached:

http://dl.dropbox.com/u/418769/storyboard%20flow.png

intro video -> welcome view & tutorial view (if username !exist) -> main view controller

this is the main file that user will alway go to.

http://dl.dropbox.com/u/418769/scannerViewController.h

http://dl.dropbox.com/u/418769/scannerViewController.m

i'm using a custom segue to pop viewcontrollers, which solved part of the problem.

-(void)perform {
    UIViewController *sourceVC = (UIViewController *) self.sourceViewController;
    NSInteger index = -1;
    NSArray* arr = [[NSArray alloc] initWithArray:sourceVC.navigationController.viewControllers];
    for(int i=0 ; i<[arr count] ; i++)
    {
        if([[arr objectAtIndex:i] isKindOfClass:NSClassFromString(@"mainViewController")])
        {
            index = i;
        }       
    }    

    [UIView transitionWithView:sourceVC.navigationController.view duration:0.5
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{

                        [sourceVC.navigationController popToViewController:[arr objectAtIndex:index] animated:NO];

                    }
                    completion:^(BOOL  completed)
     {     

             }                      
     ];

}

however, the app are still eating up the RAM and VRAM.

I really appreciate any friends here to help solving my question, does Strong value caused this problem ?

回答1:

When you say that you're using a "custom segue to pop to the Main View Controller", I'm not sure if I quite understand that. Are you using performSegueWithIdentifier? If so, then you're not popping; you're pushing another copy of the main view controller!

In most storyboards, you don't see a segue out of the right side child view looping back to the left of the parent view (and your screen snapshot showed a dizzying number of segue's entering back into the main view controller, which is a bit of a red flag). This is a far more customary storyboard (taken from Beginning Storyboards in iOS 5) at Ray Wenderlich):

Usually you'll dismiss a child view with something like the follow, not use a segue.

[self.navigationController popViewControllerAnimated:YES];

Of, if you wanted to pop back multiple levels, you would use popToViewController or popToRootViewControllerAnimated. Or if you use modal segues, you would dismiss the modal with dismissViewControllerAnimated.

If I've misunderstood what you mean't by "custom segue to pop", can you provide what code you're using do to that?



回答2:

Computer-aided analysis is the way to solve this. Do Build and Analyze and resolve all issues. Run your app under the Leaks and Allocations instruments. Use heap-shot analysis.



回答3:

  1. @ken-thomases analysis is spot on. Also see Finding leaks.

  2. I presume you are using ARC?

  3. Can you explain what is the purpose of the above code sample and what your app is doing to require something like this? It feels like you're addressing a symptom rather than solving a problem.

  4. In answer to your question, the use of strong is unlikely to be the source of problems, unless you have strong reference cycles (see Transitioning to ARC). If you follow Ken's suggestions, you'll identify where your app is leaking memory (assuming it is), and if so, where it is. At that point, if you still don't understand the source of the leak, you can post the offending code here and I'm sure there will be plenty of people willing to help. Also, if you have some code that you wonder whether the strong reference is inappropriate, post that relevant code and, again, I'm sure we'll be happy to help if we can.