【发布时间】:2014-03-24 12:14:59
【问题描述】:
我已经调试了一天左右的 iOS 应用程序,试图解决 EXC_BAD_ACCESS (SIGSEGV) 崩溃问题。
这是崩溃日志中有趣的部分:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x00000051
Triggered by Thread: 0
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x38bfc6e4 __kill + 8
1 myApp 0x002e3ca0 CLSSignalHandler + 192
2 libsystem_platform.dylib 0x38c61060 _sigtramp + 40
3 UIKit 0x306416a2 -[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:] + 310
4 UIKit 0x306417d0 __85-[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:]_block_invoke + 104
5 UIKit 0x306416e2 -[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:] + 374
6 UIKit 0x30641af2 -[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:] + 30
7 UIKit 0x308d84b8 __UIViewWasRemovedFromSuperview + 184
8 UIKit 0x30640bae -[UIView(Hierarchy) removeFromSuperview] + 266
9 UIKit 0x30643402 -[UIView dealloc] + 362
10 CoreFoundation 0x2de00650 CFRelease + 552
11 CoreFoundation 0x2de0bb40 -[__NSArrayM dealloc] + 152
12 libobjc.A.dylib 0x38649b06 objc_object::sidetable_release(bool) + 170
13 libobjc.A.dylib 0x3863b002 (anonymous namespace)::AutoreleasePoolPage::pop(void*) + 354
14 CoreFoundation 0x2de0397c _CFAutoreleasePoolPop + 12
15 UIKit 0x3063b248 _wrapRunLoopWithAutoreleasePoolHandler + 32
16 CoreFoundation 0x2de9b1ca __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 18
17 CoreFoundation 0x2de98b6c __CFRunLoopDoObservers + 280
18 CoreFoundation 0x2de98eae __CFRunLoopRun + 726
19 CoreFoundation 0x2de03c22 CFRunLoopRunSpecific + 518
20 CoreFoundation 0x2de03a06 CFRunLoopRunInMode + 102
21 GraphicsServices 0x32af727e GSEventRunModal + 134
22 UIKit 0x306a7044 UIApplicationMain + 1132
23 myApp 0x000c4640 main (main.m:14)
24 myApp 0x000ac31c start + 36
不幸的是 NSZombieEnabled 没有帮助,因为在设置时崩溃不再发生。
从我的研究中我发现:
- 它可能来自被释放两次的对象(日志中存在 Release 和 AutoRelease 内容)
- NSZombie 对象在收到
release消息时不会记录任何内容
在将问题的根源缩小到 1 个类之后,我查看了变量和属性的分配和释放。我在viewDidLoad 找到了这个:
MyView *view = [[MyView alloc] initWithFrame:CGRectMake(...)];
[_tableView addSubview:view]; // _tableView is an outlet variable
_refreshHeaderView = view; // _refreshHeaderView is a class variable, not a property
[view release];
在viewDidUnload:
[_refreshHeaderView release];
_refreshHeaderView = nil;
我将_refreshHeaderView = view; 更改为_refreshHeaderView = [view retain];,现在应用不再崩溃了。
我不太确定这是正确的解决方案,尽管崩溃不再发生,所以我有这些问题:
- 我对正在发生的事情的理解是:
-
view已初始化,其 RC=1 -
view被添加为_tableViewRC=2 的子视图 -
_refreshHeaderView与view指向同一个对象 -
view已发布 RC=1 - 当dealloc进程启动时,
_tableView将释放viewRC=0 -
_refreshHeaderView发布时,应用崩溃 - 正确吗?
-
- 为什么崩溃日志显示自动释放内容,尽管它没有与我的变量一起使用?
- NSZombies 在收到发布消息时不会记录任何错误是真的吗?
- 哪个更好?
-
_refreshHeaderView = [view retain];和[_refreshHeaderView release];或 -
_refreshHeaderView = view;和 不[_refreshHeaderView release];
-
感谢您的帮助。
【问题讨论】:
-
更好的是使用一个属性,它会为你管理内存并在
dealloc中使用self.refreshHeaderView = nil;。最好还是使用 ARC...
标签: ios objective-c