3

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 ?

Desmond
  • 5,001
  • 14
  • 56
  • 115

3 Answers3

3

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.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
3

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):

Sample storyboard from Ray Wenderlich Tutorial
(source: cloudfront.net)

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?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • i link a custom segue from other viewcontroller to the mainviewcontroller which call this file http://dl.dropbox.com/u/418769/popToMain.m which in turn it will pop back to mainviewcontrol, is this the correct way of doing in storyboard ? – Desmond May 12 '12 at 15:19
  • 1
    I guess it does work, though it strikes me as unnecessarily complicated. I'd have my main view controller be the root (and if I needed to show the welcome/tutorial, I'd make those child views that would kick off in that rare case that you actually need it). That way, a simple popToRootViewController will always do it for you and you can eliminate that silliness of trying to purge the welcome/tutorial out of the list of view controllers. – Rob May 12 '12 at 15:52
  • 1
    I'd certainly try dramatically simplifying the custom segue and just use popToRootViewController, as outlined in my prior comment, and see if that solves your RAM eating situation. If not, you then need to go back to our original comments about using instruments and see if there are any leaks or, if not, at least see what's consuming your RAM. Have you looked for leaks/allocations yet? What have you found? – Rob May 12 '12 at 16:01
  • thanks for the comments, try to simplify the custom segue. i used the heap-shot and there are 100 - 300kb per heap-shot remaining which i think is not normal – Desmond May 12 '12 at 16:06
  • The leak analysis will be enlightening, too. Personally, that's where I'd start. – Rob May 12 '12 at 21:35
  • hi Robert, i have another question. how do i use popToViewController to pop to the desire view controller i wan ? i updated my question – Desmond May 15 '12 at 06:48
  • 1
    @Desmond, you could either (a) pass it as a property in your prepareForSegue, (b) save it as a property of a singleton class, or (c) save it as a property of your app delegate that you retrieve by [UIApplication sharedApplication].delegate. Even better, if you're trying to get to you main view controller, but you're concerned about your intro view controllers, you might want to reorder your storyboard so that the main view controller is actually the root (which fires up intro when you need it), thus popToRootViewController will always take you home. – Rob May 15 '12 at 13:14
  • 1
    @Desmond By the way, if this was an iOS 6 only app, you could use unwind segues, which greatly simplify this process. Personally, I'm not quite ready to give up iSO 5 backward-compatibility, but if you are, unwind segues get the job done quite elegantly. – Rob Dec 16 '12 at 17:02
2
  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.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • hi @robert, thanks for the reply. Yes i'm using ARC had edited my question with your question i also posted one of my code that all the user will go through for the QR code scanning. – Desmond May 11 '12 at 08:35
  • 1
    Thanks. I see you do a lot of performSegueWithIdentifier calls. I also see that your storyboard has a ton of segues to enter back to the _Main View Controller._ Are you ever popping/dismissing your various view controllers in order to get back to Main View Controller, or are you just performing another segue to get there. (If you're doing the latter, i.e. you have a circular set of segues, that could eat up your memory.) – Rob May 11 '12 at 13:28
  • i'm using custom segue to pop to the Main View Controller, the mainViewController is in the nav viewcontroller stack. i pop the view back to the one which had the same name as mainviewcontroller in stack as shown in my question – Desmond May 12 '12 at 14:46