gpt4 book ai didi

ios - iPhone - 内存泄漏 - NSData dataWithContentsOfUrl 和 UIWebView

转载 作者:可可西里 更新时间:2023-11-01 04:23:06 30 4
gpt4 key购买 nike

求助!已经盯着这个看了 12 个小时;并在网上查找但找不到解决方案。

在我的应用程序中,我在单独的页面/ Controller 中使用了 2 个 UIView 控件:

  • UIImageView(检索数据通过NSData dataWithContentsOfUrl)
  • UIWebView

只是为了隔离我的代码,并使其更易于解释,我创建了一个名为“MyTestApplication”的基于 View 的新项目

1 - 我在委托(delegate)函数中添加了一个简单的 NSData dataWithContentsOfUrl。

NSData *imageData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://www.google.com/intl/en_ALL/images/logo.gif"]];

(这里没有什么可发布的,因为它都使用了方便的功能)

alt text http://img.skitch.com/20081110-j5tn5n7ixph95ys4rpchibaw5p.preview.jpg

View Image

2 - 运行它以验证没有泄漏(如预期的那样)

alt text http://img.skitch.com/20081110-fy2qrkgy47hm4fe2f1aakd4muw.preview.jpg

View Image

3 - 打开 ViewController.xib 并简单地从库中添加一个 UIWebView(无需连接它)

alt text http://img.skitch.com/20081110-d63c3yh1a1kqiciy73q8uyd68j.preview.jpg

View Image

4 - 运行它以验证是否存在泄漏! (为什么???)

alt text http://img.skitch.com/20081110-qtxcfwntbcc3csabda3r6nfjg6.preview.jpg

View Image

我做错了什么?请帮忙!

如果我使用 UIWebView,为什么 NSData 会导致内存泄漏?我只是不明白。谢谢。

最佳答案

我还遇到了 iPhone 模拟器中 NSData 的 dataWithContentsOfURL: 泄漏问题。我发现当我使用其他便捷方法 (dataWithContentsOfURL:options:error:) 时,我不会发生内存泄漏。

我的代码看起来像这样:

NSURL *url = [NSURL URLWithString:urlString];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfURL:url
options:0
error:&error];

Link to documentation

关于ios - iPhone - 内存泄漏 - NSData dataWithContentsOfUrl 和 UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/280053/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com