0

Dear all, I have a navigation-based app with about 60 views.

I have run with the following : 1. Build and analyse : bulid is successful with no complains. 2. Instruments allocation and leaks : no leaks.

However, the app crashed in iPhone or iPad but works fine in simulator. The crash occurs at around 50th view. There is no crash reports but I do see LowMemory.log in the crashreporter folder.

I have upgraded my iphone and ipad to 4.2

Does anyone have ideas what could be wrong? I have been reading and troubleshooting for a week.

Thank you for all the replies.

My app has a root view called contentViewController and users can navigate to 4 quizzes from here.

This is the code I use to return to my root view.

- (void)goHome {
UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle: @"Warning"
                      message: @"Proceed?"
                      delegate: self
                      cancelButtonTitle:@"Yes"
                      otherButtonTitles:@"No",nil];
[alert show];
[alert release];

}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[[self navigationController] setNavigationBarHidden:NO animated:YES];
if (buttonIndex == 0) {
    NSArray * subviews = [self.view subviews];
    [subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.view = nil;
    if (self.contentViewController == nil)
    {
        ContentViewController *aViewController = [[ContentViewController alloc]
                                                  initWithNibName:@"ContentViewController" bundle:[NSBundle mainBundle]];
        self.contentViewController = aViewController;
        [aViewController release];
    }
    [self.navigationController pushViewController:self.contentViewController animated:YES]; 
}

}

Sample code for pushing views :

-(IBAction) buttonArrowClicked:(id)sender {
NSURL *tapSound   = [[NSBundle mainBundle] URLForResource: @"click"
                                            withExtension: @"aif"];

// Store the URL as a CFURLRef instance
self.soundFileURLRef = (CFURLRef) [tapSound retain];

// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (

                                  soundFileURLRef,
                                  &soundFileObject
                                  );
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if (![[defaults stringForKey:@"sound"] isEqualToString:@"NO"]) {
    AudioServicesPlaySystemSound (soundFileObject);
}       

if (self.exercise2ViewController == nil)
{
    Exercise2ViewController *aViewController = [[Exercise2ViewController alloc]
                                                initWithNibName:@"Exercise2ViewController" bundle:[NSBundle mainBundle]];
    self.exercise2ViewController = aViewController;
    [aViewController release];
}
[self.navigationController pushViewController:self.exercise2ViewController animated:YES];   

}

Ian
  • 1
  • 5

2 Answers2

3

You will normally not run into memory problems when running under the simulator, so these errors are not automatically encountered on this platform.

The simulator does however have a feature where you can manually trigger a Low Memory event. If this is actually the cause of the crash on the device, then it might also be possible that you can trigger the same bug in the simulator in this way.

Claus Broch
  • 9,212
  • 2
  • 29
  • 38
0

Sharing some code about how you push the view controllers will allow others to help you with this.

You can pop to root view controller more easily by doing:

[self.navigationController popToRootViewControllerAnimated:YES];

You are actually pushing a new instance of your root view controller in the code that you have shared.

Chintan Patel
  • 3,175
  • 3
  • 30
  • 36
  • Thanks for all replies. I have added some codes for your review – Ian Nov 26 '10 at 02:45
  • Sorry realized that I have double posted. Please refer to http://stackoverflow.com/questions/4281713/iphone-app-crashes-due-to-low-memory-but-works-fine-in-simulator – Ian Nov 26 '10 at 03:17
  • Thank you very much for saving me. My app doesn't seem to crash now! It still crashes in my devices when I run in debug mode. However, it runs well in my devices when connected. Is this normal? – Ian Nov 26 '10 at 08:05
  • That might be a different issue like not releasing the view controller after pushing it. For every [self.navigationController pushViewController:viewController animated:YES]; there must be a [viewController release]; immediately after it unless you really know what you are doing. – Chintan Patel Nov 26 '10 at 08:33
  • Sorry I meant it runs well in devices when disconnected. Please see my sample code above. Do u mean I have to add [exercise2ViewController release]; at the end? Currently my [exercise2ViewController release]; is in the dealloc method. Thanks alot for your patience. – Ian Nov 26 '10 at 14:07
  • Debugging the app does consume some extra memory on the device but i think you also have some other problems if memory consumption is reaching levels that the debugger has to quit. Try running your app with Instruments to check for memory usage. – Chintan Patel Nov 26 '10 at 14:12
  • 1 last question. If I want to reset my quizz, how can I return to root view with [self.navigationController popToRootViewControllerAnimated:YES]; and at the same time remove all the views? – Ian Nov 26 '10 at 14:20
  • When you do "pushViewController", the navigation controller pushes that controller on its stack and owns that object. So you can release the view controller immediately after that. Its the same as doing [array addObject:someObject]; [someObject release];. In dealloc method, it will only get released when that controller gets deallocated. By popping the view controller, the navigation controller will relinquish its ownership of the object and it will call dealloc method ONLY if the retain count gets zero. – Chintan Patel Nov 27 '10 at 07:51