gpt4 book ai didi

释放包含滚动 UITableView 的 View Controller 时,iPhone 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 20:56:17 31 4
gpt4 key购买 nike

这是一个非常非常...非常奇怪的错误。很难描述我所拥有的确切项目,但我会尝试创建我所拥有的类的更简单的表示。事情是这样的:

假设我有一个导航 Controller 作为我的顶 View Controller 。在其中,某一时刻我有一个 UIViewController,比方说是一个 ContactsScreenController。该 View 包含多个 UITableView,每个 UITableView 都由类型为 MyTableController(委托(delegate)和数据源)的单独对象控制。我通过保留 Controller 数组来做到这一点


// This is the interface for my screen controller. An object of this type goes in a top-
// level navigation controller
// MainScreenController.h
@interface ContactsScreenController : UIViewController

NSMutableArray* tableControllers;

@end

<小时/>

// MainScreenController.m

- (UITableViewCell*)cellForRowAtIndexPath..something..
{
// Here what I do is create a new controller if needed, and add it to tableControllers
// Memory allocations & releases are good because I checked with instruments
}

#define SAFE_DEL(x) { if (x != nil) { [x release]; x = nil; } }

- (void)dealloc
{
SAFE_DEL(tableControllers);
[super dealloc];
}

现在,MyTableController 是一个更复杂的对象,因为它处理从 Web 服务获取数据,但基本上我要做的是确保当对象被删除时,我取消任何挂起的数据请求,如下所示:


// MyTableController.m
- (void)dealloc
{
[globalDataProvider cancelRequestsForController:self];

// release other objects i might have
[super dealloc];
}

好的,这就是我的对象设置。当我删除对象 tableControllers 时发生崩溃。它减少了 MyTableController 对象的保留计数,并达到 0(使用 Instruments 检查)。但由于某些未知的原因,在保留计数为零之后,我收到了 cancelRequestsForController 的调用。显然,我获得了 EXC_BAD_ACCESS。

在您开始认为这是我的保留/释放对的问题之前,如果我在内部表是静态的情况下释放主屏幕 Controller ,则应用程序可以完美运行。一旦滚动并且我点击导航 Controller 中的“后退”按钮,我就会遇到该错误。

我已经使用仪器检查了我的内部 Controller 的保留计数更改的整个历史记录,这很好(没有不寻常的东西)。当错误发生时,我历史记录中的最后一个条目来自 QuartzCore run_animation_callbacks,保留计数为 -1。

有什么想法吗? :)

PS:作为启动项目的快速解决方案,我已将 cancelRequestsForController 移至单独的方法中,并在发布之前为 tableControllers 中的每个对象手动调用它。这样我就可以确定,无论tableview的状态如何,释放后都不会再有任何调用。


- (void)dealloc
{
for (TableController* c in tableControllers)
[c cancelRequests];
SAFE_DEL(tableControllers);
[super dealloc];
}

但出于多种原因,我不喜欢这个解决方案。

最佳答案

感谢大家的回答/评论。
该问题是由在我有机会释放对象之前调用对象中的 [super dealloc] 产生的。这导致了很多疯狂的事情发生。我将 [super dealloc] 移到了 dealloc 方法的末尾(在我发布之后),现在它工作正常。

关于释放包含滚动 UITableView 的 View Controller 时,iPhone 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5024139/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com