UIWebView内存问题

发布于 2024-10-07 11:12:53 字数 566 浏览 0 评论 0原文

我正在 UIWebView 上播放 youtube 视频,它显示为 modalViewController 子视图(翻转过渡)。一切工作正常,即使 UIWebView 已发布,在多次重复选择此 modalViewController 后我仍然收到内存警告。

我已以编程方式在 ViewDidLoad 中添加了 UIWebView。在 viewDidDisappear 内,我检查 [UIWebView keepCount],如果大于 1,则执行以下步骤:

[[NSURLCache sharedURLCache] removeAllCachedResponses]; 
[self.webView removeFromSuperview];
self.webView.delegate = nil;
self.webView = nil;

NSLog(@"[self.webView retainCount] %d", [self.webView retainCount]);

我在 xCode 3.2.5、iOS 4.2 上运行我的代码。

感谢您提供的所有帮助。

I am playing youtube videos on a UIWebView which appears as a modalViewController subview (flip transition). Everything works fine, even though the UIWebView is released, I still receive memory warnings after a few repeated selection of this modalViewController.

I have added my UIWebView programmatically inside ViewDidLoad. Inside viewDidDisappear I check for [UIWebView retainCount] and if greater than 1, perform the following steps:

[[NSURLCache sharedURLCache] removeAllCachedResponses]; 
[self.webView removeFromSuperview];
self.webView.delegate = nil;
self.webView = nil;

NSLog(@"[self.webView retainCount] %d", [self.webView retainCount]);

I am running my code on xCode 3.2.5, iOS 4.2.

Appreciate all you help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想挽留 2024-10-14 11:12:53

我认为您正在以错误的方式处理内存管理问题。如果您知道自己在做什么,则检查保留计数是一种有效的调试技术。然而,它不是内存管理工具。在您的特定情况下,如果正在显示 UIWebView,它将始终具有保留计数 > 。 1. 超级视图将有一个保留,从而使“if”毫无用处。

如果 webView 属性定义良好(即 noatomic、retain),则语句:

 self.webView = nil;

应该释放 webView。一个常见的错误是用以下方式初始化属性:

self.webView = [[UIWebView alloc] init];

如果 webView 被定义为“retain”,则可能会导致泄漏。正确的方法是,

self.webView = [[[UIWebView alloc] init] autorelease];

如果您无法在不耗尽内存的情况下多次显示控制器,则说明存在内存泄漏。使用仪器(特别是泄漏)来查找正在正确释放的对象。 这是一个很好的教程

小心保持保留和释放平衡并检查是否有泄漏。

I think you are approaching the memory management problem in the wrong way. Checking the retainCount is a valid debugging technique if you know what you are doing. It is not, however, a memory management tool. In your particular case, if the UIWebView is being displayed it will always have retain count > 1. The superview will have a retain on it thus making the "if" useless.

If the webView property is well defined (i.e. noatomic, retain) the statement:

 self.webView = nil;

should release the webView. A common mistake is to initialize the property with:

self.webView = [[UIWebView alloc] init];

This is likely to introduce a leak if the webView is defined as "retain". The correct way is

self.webView = [[[UIWebView alloc] init] autorelease];

If you can't display your controller several times without running out of memory you have a memory leak. Use Instruments (Leaks in particular) to find hte objects what are note being released properly. This is a good tutorial.

Be careful in keeping your retains and releases balanced and check for leaks.

离旧人 2024-10-14 11:12:53

您的问题将与此相关:
是否可以阻止 NSURLRequest 缓存数据或删除请求后的缓存数据?

向下滚动到我的答案以获取已接受答案的扩展 - 我已经遇到这个问题好几天了,现在已经解决了!

Your problem will be related to this:
Is it possible to prevent an NSURLRequest from caching data or remove cached data following a request?

Scroll down to my answer for an extension of the accepted answer - i had this problem for days and it's now resolved!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文