2

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 .

Pooja M. Bohora
  • 1,311
  • 1
  • 14
  • 42
Kunal Balani
  • 4,739
  • 4
  • 36
  • 73
  • What are you doing in the launchExecution which you detach to a separate thread? Are you accessing any nonatomic properties that might result in unexpected application behavior because of inconsistent state. Are you updating the UI in any way, or calling methods that might -- because UI must only be updated on the main thread. My hunch is that it's something hokey with launchExecution being called on a separate thread. Any chance you could stick this in a block and execute it with Grand Central Dispatch instead? – Philip Jun 17 '12 at 03:13
  • are you accessing any UI elements from your separate thread..? – Ankit Srivastava Jun 17 '12 at 04:00
  • [appDelegate showHUDActivityIndicatorOnView:self.parentViewController.view whileExecuting:@selector(refreshInBackground) onTarget:self withObject:nil withText:@"Loading"]; where it shows the MBProgress HUD view and executes refresh in background(a call to web service API) in different thread. And it works fine ... The issue arises when I call it several times .. I guess that number is fixed .. when I replace the above code with [self refreshinbackground] // this is on main thread .. everything works fine .... no issues ... – Kunal Balani Jun 17 '12 at 16:41
  • @Philip I dont know why is my app crashing just when it has used memory between 4.3 - 4.6 MB... it crashes always there .. it's the os who is killing my app – Kunal Balani Jun 17 '12 at 16:44
  • @AnkitSrivastava In that case I would have got an run time error ... but I dont think I have any such case ... – Kunal Balani Jun 17 '12 at 19:00

2 Answers2

3

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 ...

NightFury
  • 13,436
  • 6
  • 71
  • 120
Kunal Balani
  • 4,739
  • 4
  • 36
  • 73
0

If you're not using ARC, you have for sure a leak every time you assign the property:

self.HUD = [[MBProgressHUD alloc] initWithView:view] ;

Change your code in this way:

MBProgressHUD *progressHUD = [[MBProgressHUD alloc] initWithView:view] ;
self.HUD = progressHUD;
[progressHUD release];
Valerio
  • 513
  • 2
  • 14
  • I have tried with autorelease .. didn't work ... Even if it's a memoory leak just the usage of 4.5 Mb of heap memory should not crash my app ... I agree my code has memory leaks but the crash for constantly iOS killing my app at 4.5 Mb is something else ..not just memory leak – Kunal Balani Jun 17 '12 at 16:49
  • How many progress indicators do you have? is it possible you have critical sections executed at the same time by multiple thread? – Valerio Jun 18 '12 at 06:53
  • I am just displaying one at a time ... Also the the properties are atomic ... so I doubt this could be an issue but possible. ... I haven't done through testing to check that ... will try to put synchronized block and see the results ... – Kunal Balani Jun 18 '12 at 15:51