UIWebView loadRequest 导致模拟器崩溃
所以我尝试将一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请勿调用
dealloc
。改为在url
和urlString
上调用release
。这是因为其他对象引用了这些对象,当您说
dealloc
时,您正在显式销毁它们。这导致了EXC_BAD_ACCESS
,因为当其他对象尝试访问 url 和 string 对象时,它们已经被销毁。这就是引用计数的全部要点。你已经完成了这些对象,所以如果你说:
你正在声明这一点。这会减少对这些对象的引用计数。但在上面,您说过:
这意味着
url
可能有对该字符串的引用。因此,当您创建它时,它会保留
它。因此,在释放字符串后,它不会被销毁,因为url
仍然有对其的引用。当 IT 使用完该对象后,它也会释放它,然后(如果没有其他人拥有该对象),它将自动被释放
,因为它的计数将降至零。当您处理这种内存管理时,请始终记住还有谁可能正在使用您的对象。您可以在Apple 文档。
DO NOT CALL
dealloc
. Callrelease
onurl
andurlString
instead.This is because other objects have references to those objects, and when you say
dealloc
, you are explicitly destroying them. This is causing theEXC_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:
You are declaring that. This decrements the count of references to those objects. But up above, you said:
This means
url
probably has a reference to that string. So it would haveretain
ed it when you created it. So after you release the string, it does not get destroyed becauseurl
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 bedealloc
ed, 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.