Search code examples
iphoneiosxcodememory-managementmbprogresshud

Application Crash due to Memory issue / MBProgressHUD Indicator


I have a strange issue . I am currently working on a mail app and it used to crash randomly without and error or logs in my console . I checked my Crash Log it showed me Low Memory warning with jettisoned written next to my App. Crash Report

So I suspect it's an memory issue and went back to trace my memory usage of my application. I used allocation instrument to detect the overall usage and my application crashed when it's heap size was just 4.59 MB. Instrument snapshot

Instruments point towards a function where I am using MBProgressHUD indicator. The culprit is this one line :

[appDelegate showHUDActivityIndicatorOnView:self.parentViewController.view whileExecuting:@selector(refreshInBackground) onTarget:self withObject:nil withText:@"Loading"];

If I replace this with [self refreshInBackground] everything works fine no issues ..

Here is the code :

    -(void) showHUDActivityIndicatorOnView:(UIView*)view whileExecuting:(SEL)method 
                                   onTarget:(id)target withObject:(id)object withText:(NSString*)label{
    self.HUD = [[MBProgressHUD alloc] initWithView:view] ;
        self.navigationController.navigationItem.hidesBackButton = YES;
        [view addSubview:self.HUD];
    self.HUD.delegate = nil;
    self.HUD.labelText = label;
    [self.HUD showWhileExecuting:method onTarget:target withObject:object animated:YES];    
}

self.HUD is a property which is being retained.

With a slight modification is showWhileExecuting method as follows :

- (void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object animated:(BOOL)animated {

    MyAppdelegate *appDelegate = (MyAppdelegate *)[UIApplication sharedApplication].delegate;
    if(!appDelegate.isRequestActive)
    {
    methodForExecution = method;
    targetForExecution = [target retain];
    objectForExecution = [object retain];

    // Launch execution in new thread
    taskInProgress = YES;
    [NSThread detachNewThreadSelector:@selector(launchExecution) toTarget:self withObject:nil];

    // Show HUD view
    [self show:animated];
    }
    else {
        [self done];
    }
}

currently I have removed this from my app and it works fine now and it does not crashes even with the usage of 20 - 30 MB heap Memory .

I am not looking for an specific answer or solution here . What I am looking for ways to debug / techniques to debug the issue so that I can get to know what caused my app to crash .

Is it memory overflow . If that's the case then how can I use 20-30 MB right now. If it's not an memory issue why does my crash reporter shows jettisoned next to my App name .

the culprit line ([appDelegate showHUDActivityIndicatorOnView:self.parentViewController.view whileExecuting:@selector(refreshInBackground) onTarget:self withObject:nil withText:@"Loading"])

Every time when I call this function some memory increases , because of some elements being cached . But when memory reached 4.5 MB ...this line cause it to crash

How do I get to the root cause of this issue . How do I figure out why iOS is killing my app the reason for jettisoned written next to my app

Any help or suggestions would be really appreciated .


Solution

  • Ok. The problem is I was adding the HUD progress bar on my view using [view addSubview:self.HUD];

    I forgot to remove it from the super view in its delegate method:

    - (void)hudWasHidden:(MBProgressHUD *)hud 
    {
        // Remove HUD from screen when the HUD was hidded
        [HUD removeFromSuperview]; //  app crashes at 4.59 MB if you comment this
        [HUD release];
        HUD = nil;
    }
    

    Because of this several views were added every time on one UIView ... I guess there is an upper limit for the number of child subviews on top of each UIView .... apple should mention this in their documentation ...