使用MBProgressHUD发生内存泄漏 [英] memory leak using MBProgressHUD
问题描述
我正在使用MBProgressHUB,并在这里.
I am using the MBProgressHUB, with variations on the code found here.
关于我的代码的一些事情:
Some things about my code:
- 代码在我的应用程序委托中
- 许多其他的类称呼它
- 我将其与异步NSURLConnection一起使用
- 我不声明它:@property(非原子性,保留)MBProgressHUD * HUD;
- 我不:@synthesize HUD;
- (当然,我也不会在我的dealloc中释放它)
- 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)
我按如下方式使用它:
- (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:
异常类型:EXC_BAD_ACCESS(SIGBUS)
异常代码:0x0000000c处的KERN_PROTECTION_FAILURE
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000c
0 libobjc.A.dylib 0x000027d8 objc_msg发送+ 16
1我的0x00003120-[MyAppDelegate setSearchingMode:](MyAppDelegate.m:363)
2我的0x00003458-[MyAppDelegate connectionDidFinishLoading:](MyAppDelegate.m:341)
3 Foundation 0x00032896-[NSURLConnection(NSURLConnectionReallyInternal)sendDidFinishLoading] + 62
4基金会0x00032818 _NSURLConnectionDidFinishLoading + 72
0 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
在connectionDidFinishLoading中,我打电话:
in connectionDidFinishLoading I call:
[self setSearchingMode:NO];
我试图通过为HUD设置属性访问器来做到这一点,但无法绕过"[MBProgressHUD alloc] initWithWindow"这一行-我不想继续分配ivar!
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:
[self setSearchingMode:YES];
[self setSearchingMode:NO];
[self setSearchingMode:NO];
由于对HUD
的悬挂引用,该代码将按照描述的那样崩溃.当您执行[HUD release];
时,请在该行之后添加HUD = nil;
.
That code will crash as described because of the dangling reference to HUD
. When you do [HUD release];
, add HUD = nil;
after that line.
这不是内存泄漏;这是一个过度发行.或者更可能是悬挂的参考.
It isn't a memory leak; it is an over-release. Or, more likely, a dangling reference.
(当然,我不会在 我的dealloc)
(and of course I do not release it in my dealloc)
为什么不呢?如果保留它,最好将其释放!
Why not? If you retain it, you better release it!
这篇关于使用MBProgressHUD发生内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!