@implementation FeedbackViewController - (void)viewDidLoad { [super viewDidLoad]; _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 53, 320, 389)]; [self.view addSubview:_scrollView]; [_scrollView release]; } - (void)dealloc { [_scrollView release]; [super dealloc]; } @end
_scrollView
是
FeedbackViewController
中的一个数据成员。
_scrollView 创建后先加入到父视图中,然后将其
release,此时
_scrollView 的引用计数应该为1,在父视图销毁时,
_scrollView 会被释放掉,这正是我们期望的结果。 但是
FeedbackViewController 的
dealloc 函数中,对
_scrollView 对象又进行了一次
release,很显然这会造成对象的多次释放。 执行上面的代码,在
FeedbackViewController 退出时,
[_scrollView release]; 会引起程序异常退出,且在控制台中输出
EXC_BAD_ACCESS :

问题解决
打开可执行文件的属性:


2012-03-15 22:02:12.029 AlipayPortal[616:207] *** -[CALayer retainCount]: message sent to deallocated instance 0x14e48a40这个输出中有个很重要的信息就是定位到了无效对象在内存中的地址,我们已经打开了
MallocStackLoggin
选项,编译器会记录该内存的分配堆栈。在控制台中输入以下命令:
(gdb) info malloc-history 0x14e48a40会得到以下输出:
