UIWebView loadRequest 导致模拟器崩溃

发布于 2024-10-27 06:10:52 字数 742 浏览 1 评论 0原文

所以我尝试将一个简单的 URL (http://www.google.com) 加载到网络视图中。我在我正在使用的应用程序中拥有这个,但将其归结为一个基本应用程序以删除其他变量。我有一个绝对最小的应用程序设置,一个视图控制器,以 web 视图作为其主要视图。对于我的代码

-(void)viewDidLoad
{
    [super viewDidLoad];

    UIWebView *webView = (UIWebView*)self.view;
    NSMutableString *urlString = [[NSMutableString alloc] initWithString:@"http://www.google.com"];
    NSURL* url = [[NSURL alloc] initWithString:urlString];
    NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url];
    NSLog(@"Sending web request to %@", urlString);

    [webView loadRequest:urlRequest];

    [urlRequest release];
    [url dealloc];
    [urlString dealloc];
}

,当它加载时,我在 Web 线程中遇到 EXC_BAD_ACCESS 崩溃。我不确定这是否是与我在模拟器中工作有关的问题,或者是我刚刚搞砸了设置的问题。

So I'm trying to load a simple URL (http://www.google.com) into a web view. I had this in the application I was working with, but boiled it down to a basic application to remove other variables. I have an absolute minimal application setup, one view controller with a webview as its primary view. For code I have

-(void)viewDidLoad
{
    [super viewDidLoad];

    UIWebView *webView = (UIWebView*)self.view;
    NSMutableString *urlString = [[NSMutableString alloc] initWithString:@"http://www.google.com"];
    NSURL* url = [[NSURL alloc] initWithString:urlString];
    NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url];
    NSLog(@"Sending web request to %@", urlString);

    [webView loadRequest:urlRequest];

    [urlRequest release];
    [url dealloc];
    [urlString dealloc];
}

And when it loads, I get an EXC_BAD_ACCESS crash within the Web Thread. I am unsure if this is a problem related to the fact that I am working in the simulator, or something I've just screwed up with the setup.

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

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

发布评论

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

评论(1

养猫人 2024-11-03 06:10:52

请勿调用dealloc。改为在 urlurlString 上调用 release

这是因为其他对象引用了这些对象,当您说dealloc时,您正在显式销毁它们。这导致了 EXC_BAD_ACCESS,因为当其他对象尝试访问 url 和 string 对象时,它们已经被销毁。

这就是引用计数的全部要点。你已经完成了这些对象,所以如果你说:

[url release];
[urlString release];

你正在声明这一点。这会减少对这些对象的引用计数。但在上面,您说过:

NSURL* url = [[NSURL alloc] initWithString:urlString];

这意味着 url 可能有对该字符串的引用。因此,当您创建它时,它会保留它。因此,在释放字符串后,它不会被销毁,因为 url 仍然有对其的引用。当 IT 使用完该对象后,它也会释放它,然后(如果没有其他人拥有该对象),它将自动被释放,因为它的计数将降至零。

当您处理这种内存管理时,请始终记住还有谁可能正在使用您的对象。您可以在Apple 文档。

DO NOT CALL dealloc. Call release on url and urlString instead.

This is because other objects have references to those objects, and when you say dealloc, you are explicitly destroying them. This is causing the EXC_BAD_ACCESS, because when other objects try to access the url and string objects, they have already been destroyed.

This is the whole point of reference counting. YOU are done with those objects, so if you say:

[url release];
[urlString release];

You are declaring that. This decrements the count of references to those objects. But up above, you said:

NSURL* url = [[NSURL alloc] initWithString:urlString];

This means url probably has a reference to that string. So it would have retained it when you created it. So after you release the string, it does not get destroyed because url still has a reference to it. When IT is done with it, it too will release it, and then (if no one else has claim to the object), it will automatically be dealloced, because its count will have dropped to zero.

Always keep in mind who else might be using your objects when you are dealing with this sort of memory management. You can read more about it in Apple's docs.

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