I am using the MBProgressHUB, with variations on the code found here.
Some things about my code:
- The code is in my App Delegate
- A number of other classes call it
- I’m using it with asynchronous NSURLConnection
- I do not declare it: @property (nonatomic, retain) MBProgressHUD *HUD;
- I do not: @synthesize HUD;
- (and of course I do not release it in my dealloc)
I use it as follows:
- (void)setSearchingMode:(BOOL)isSearching {
// when network action, toggle network indicator and activity indicator
if (isSearching) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
UIWindow *theWindow = [UIApplication sharedApplication].keyWindow;
HUD = [[MBProgressHUD alloc] initWithWindow:theWindow];
[theWindow addSubview:HUD];
//HUD.labelText = @"Connecting";
[HUD show:YES];
} else {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[HUD hide:YES];
[HUD removeFromSuperview];
[HUD release];
}
}
- (void)setSearchingText:(NSString *)whatToSay {
HUD.labelText = whatToSay;
}
I am fairly sure that the code is causing a memory management problem somewhere. In my crash log, I get:
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000c0 libobjc.A.dylib 0x000027d8 objc_msgSend + 16
1 My 0x00003120 -[MyAppDelegate setSearchingMode:] (MyAppDelegate.m:363)
2 My 0x00003458 -[MyAppDelegate connectionDidFinishLoading:] (MyAppDelegate.m:341)
3 Foundation 0x00032896 -[NSURLConnection(NSURLConnectionReallyInternal) sendDidFinishLoading] + 62
4 Foundation 0x00032818 _NSURLConnectionDidFinishLoading + 72
in connectionDidFinishLoading I call:
[self setSearchingMode:NO];
I tried to do it by making property accessors for HUD, but was not able to get around the line "[MBProgressHUD alloc] initWithWindow" – and I don’t want to keep on alloc’ing an ivar!
Thanks, if anyone can point me in a better direction here..
If you happen to end up with the sequence of:
That code will crash as described because of the dangling reference to
HUD. When you do[HUD release];, addHUD = nil;after that line.It isn’t a memory leak; it is an over-release. Or, more likely, a dangling reference.
Why not? If you retain it, you better release it!