UIAlertView - 显示导致内存泄漏
我对 iPhone 开发还比较陌生,所以这可能是我的错,但这与我所看到的相反。 :)
我认为我正在创建一个 UIAlertView,它就存在于“if”语句的真空中。
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(!data)
{
// Add an alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Unable to contact server"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
NSLog(@"retain count before show: %i", alert.retainCount);
[alert show];
NSLog(@"retain count before release: %i", alert.retainCount);
[alert release];
NSLog(@"retain count after release: %i", alert.retainCount);
return nil;
}
然而,控制台日志让我感到困惑。
retain count before show: 1
retain count before release: 6
retain count after release: 5
我也尝试过
alert = nil;
在发布后添加:。这使得保留计数为 0,但我仍然显示泄漏。如果有帮助,泄漏的责任框架是 UIKeyboardInputManagerClassForInputMode。我也在使用 OS 4 Beta 3。
所以有人知道调用 -show 时本地 UIAlertView 的保留计数如何自行增加 5 吗?
感谢您的帮助!
I'm relatively new to iPhone Development, so this may be my fault, but it goes against what I've seen. :)
I think that I'm creating a UIAlertView that lives just in this vaccuum of the 'if' statement.
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(!data)
{
// Add an alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Unable to contact server"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
NSLog(@"retain count before show: %i", alert.retainCount);
[alert show];
NSLog(@"retain count before release: %i", alert.retainCount);
[alert release];
NSLog(@"retain count after release: %i", alert.retainCount);
return nil;
}
However, the console logs baffle me.
retain count before show: 1
retain count before release: 6
retain count after release: 5
I've tried also adding:
alert = nil;
after the release. That makes the retain count 0, but I still show a leak. And if it helps, the leak's Responsible Frame is UIKeyboardInputManagerClassForInputMode. I'm also using OS 4 Beta 3.
So anyone have any ideas how a local UIAlertView's retain count would increment itself by 5 when calling -show?
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您意识到 [alert show] 不会立即将警报显示在屏幕上,那么这是有道理的。我认为发生的情况是 [警报显示] 将警报添加到系统中保留警报的某个队列中。 直到您从该函数返回并返回到事件循环之前,它实际上不会显示。当它最终被解除时,这些保留计数将减少,然后被释放。
如果您要从 UIAlertView 的委托例程(例如 didPresentAlertView)记录消息,我敢打赌,直到您的函数结束、“释放”警报之后,这种情况才会发生。释放并不总是意味着解除分配,它只是放弃所有权。如果没有剩余所有者,它只会导致释放。
This makes some sense if you realize that [alert show] doesn't immediately put the alert up on screen. I think what happens is that the [alert show] adds the alert to some queue somewhere in the system which retains it. It won't actually be shown until you return from this function and get back to the event loop. When it eventually gets dismissed those retain counts will get decremented and it will be released then.
If you were to log messages from UIAlertView's delegate routines, such as didPresentAlertView, I'll bet that doesn't happen until after your function ends, after you've "released" the alert. Release doesn't always mean deallocate, it is just relinquishing ownership. It only causes a dealloc if there are no owners left.
我非常怀疑这是内存泄漏。警报视图刚刚显示:它被添加到窗口等:它们都保留了它们所拥有的内容。我敢打赌,如果你在它关闭后检查,它就不再存在了。
I highly doubt this is a memory leak. The alert view is just getting shown: it is getting added to the window, etc: which all retain what they own. I bet you that if you check once it has been closed, it won't exist anymore.
我的猜测是这与 beta sdk 有关。测试版中有很多错误/bug。我建议使用 3.1.3 或 3.2 sdk 检查它。
My guess would be that this is related to the beta sdk. There are many errors/bugs in the betas. I would suggest checking it with the 3.1.3 or 3.2 sdk.