UIWebView 中的 EXC_BAD_ACCESS
我刚刚从 iTunes Connect 下载了我的一款 iPhone 应用程序的崩溃报告。最常见的崩溃有如下痕迹:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0xa1b1c1db
Crashed Thread: 0
Thread 0 Crashed:
0 libobjc.A.dylib 0x3030e6f4 objc_msgSend + 16
1 UIKit 0x30ebebee -[UIWebView webView:resource:didFinishLoadingFromDataSource:]
2 UIKit 0x30ebe5ca -[UIWebViewWebViewDelegate webView:resource:didFinishLoadingFromDataSource:]
3 CoreFoundation 0x32b73b5c __invoking___ + 60
4 CoreFoundation 0x32bc67be -[NSInvocation invoke]
5 WebCore 0x320baa86 HandleDelegateSource
6 CoreFoundation 0x32bb8a96 CFRunLoopRunSpecific
7 CoreFoundation 0x32bb8356 CFRunLoopRunInMode
8 GraphicsServices 0x30544cd4 GSEventRunModal
9 GraphicsServices 0x30544d80 GSEventRun
10 UIKit 0x30d2c768 -[UIApplication _run]
11 UIKit 0x30d2b46c UIApplicationMain
我大约 80% 确定这是 UIWebView 内部的问题,超出了我可以解决的范围。有没有人对如何缩小此问题的范围有任何建议,以帮助确定这是操作系统和 UIWebView 的问题,还是我可以在代码中修复和解决的问题?提前致谢。
更新:有问题的 UIWebView 位于一个视图中,当用户点击相应导航控制器的后退按钮时,该视图就会被释放。接受的解决方案似乎为发生此错误的原因提供了一个很好的解释。
建议的解决方案之前:
- (void)dealloc {
[webView release];
[super dealloc];
}
建议的解决方案之后:
- (void)dealloc {
webView.delegate = nil;
[webView stopLoading];
[webView release];
[super dealloc];
}
I just downloaded the crash reports for one of my iPhone apps from iTunes Connect. The most common crash has a trace like the following:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0xa1b1c1db
Crashed Thread: 0
Thread 0 Crashed:
0 libobjc.A.dylib 0x3030e6f4 objc_msgSend + 16
1 UIKit 0x30ebebee -[UIWebView webView:resource:didFinishLoadingFromDataSource:]
2 UIKit 0x30ebe5ca -[UIWebViewWebViewDelegate webView:resource:didFinishLoadingFromDataSource:]
3 CoreFoundation 0x32b73b5c __invoking___ + 60
4 CoreFoundation 0x32bc67be -[NSInvocation invoke]
5 WebCore 0x320baa86 HandleDelegateSource
6 CoreFoundation 0x32bb8a96 CFRunLoopRunSpecific
7 CoreFoundation 0x32bb8356 CFRunLoopRunInMode
8 GraphicsServices 0x30544cd4 GSEventRunModal
9 GraphicsServices 0x30544d80 GSEventRun
10 UIKit 0x30d2c768 -[UIApplication _run]
11 UIKit 0x30d2b46c UIApplicationMain
I'm about 80% sure this is an issue internal to UIWebView and outside the scope of what I can address. Does anyone have any suggestions on how to narrow down this issue to help identify whether it's an issue with the OS and UIWebView, or an issue that I can fix and address in my code? Thanks in advance.
UPDATE: The UIWebView in question is in a view that gets released when the user hits the back button of the corresponding navigation controller. The accepted solution seems to provide a good explanation for why this error is occurring.
Before suggested solution:
- (void)dealloc {
[webView release];
[super dealloc];
}
After suggested solution:
- (void)dealloc {
webView.delegate = nil;
[webView stopLoading];
[webView release];
[super dealloc];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该场景是这样的:
UIWebView
进入屏幕。UIViewController
将self
设置为委托3a.
UIViewController
被释放UIWebView
完成加载并向其委托发送“我完成”消息...您需要阻止
UIWebView
加载其页面并在取消分配委托之前将其委托设置为 nil。The scenario goes something like this:
UIWebView
. TheUIViewController
setsself
as the delegate3a.
UIViewController
gets deallocatedUIWebView
finishes loading and sends "I finished" message to its delegate...You need to stop the
UIWebView
from loading its page and sets its delegate to nil before you deallocate the delegate.你的代码几乎 100% 有错误。 iPhone SDK 中的错误非常罕见,并且
UIWebView
已被许多其他开发人员测试得非常好。EXC_BAD_ACCESS
通常在您尝试访问已释放的对象时发生。显然,如果苹果的代码试图这样做,那么你就是错误地释放了该对象的人。您确定没有太多的-autorelease
或-release
吗?It's almost 100% an error in your code. Errors in the iPhone SDK are quite rare and
UIWebView
has been tested quite good by many other developers.EXC_BAD_ACCESS
occurs usually when you try to access an already released object. Obviously if Apple's code is trying to do that you are the one who has released the object wrongly. Are you sure you don't have a-autorelease
or-release
too much?我最近遇到了类似的问题,我的应用程序随机崩溃。事实证明,问题出在加载的 HTML 中使用“
onclick=
”。将
onclick
替换为简单的,问题就消失了。
希望这可以帮助其他遇到与
webviews
相同问题的人。I recently had a similar problem where my apps were crashing randomly. Turns out the problem was in using "
onclick=
" in the loaded HTML.Replaced the
onclick
with simple<a href="javascript:some_script">
and the problem went away.Hope this helps others who are experiencing the same issue with
webviews
.仔细看看代码中实现
UIWebViewDelegate
协议的内容。特别是您想查看正在处理的内容webViewDidFinishLoad:
您正在尝试访问已释放的变量。如果可以的话,发布完整的源代码,这将帮助我们为您找到它。Take a harder look inside the thing in your code that is implementing the
UIWebViewDelegate
protocol. In particular you want to look at whatever is handlingwebViewDidFinishLoad:
You are trying to access a variable that's been released. Post the full source if you can, that will help us find it for you.我有类似的问题。我正在使用:
“str”的释放导致错误消息“EXC_BAD_ACCESS”
I had a similar problem. I was using:
The release of "str" caused the error message "EXC_BAD_ACCESS"