Objective-C 表格视图在重新加载时崩溃

发布于 2024-12-20 01:48:32 字数 673 浏览 2 评论 0原文

我在主应用程序委托中创建了一个对象数组,该委托从 sqlite 数据库加载数据。当我加载 TableView 时,此类中的属性被设置为指向对象数组。然后,当显示 TableView 时,它会正确返回数组的行数,并根据数组中相应对象的属性在单元格中显示文本。

我还在导航栏的右上角有一个刷新按钮,该按钮会从网络下载 XML 文件,对其进行解析,然后更新数据库。

然后,我从数据库重新创建对象的主数组,并将 TableView 属性指向新数组,然后要求 TableView 重新加载数据。这就是它崩溃并出现 EXC_BAD_ACCESS 的地方。

我已经使用调试器进行了单步调试,并确定在 reloadData 调用之前一切都是正确的。数据库和主数组都是同步的,自定义 TableView 属性也指向主数组。

然后,当我逐步使用调试器时,我注意到了一些事情。首先,当它调用numberOfRowsInSection时,指向主数组的指针是正确的,行数也是正确的。其次,当它到达第一个 cellForRowAtIndexPath 调用时,指向主数组的指针指向无效的内存部分。实际的内存地址是相同的,但对象数组已经消失。

我无法理解数组如何在重新加载过程中消失,因为我的引用计数应该仍然大于零,所以不应该有垃圾收集。所以我的问题是,有人知道会发生什么吗?其次,如何确定问题的具体位置?

I've created an array of objects in the main app delegate that is loading data from an sqlite database. When I load the TableView, a property in this class is set to point to the array of objects. Then, as the TableView is displayed it correctly returns the count of the array for the number of rows and displays the text in the cell from a property of the appropriate object in the array.

I also have a refresh button in the top-right corner of the navigation bar which goes out and downloads an XML file from the network, parses it, and then updates the database.

I then recreate the main array of objects from the database and point my TableView property to the new array and then ask the TableView to reload the data. This is where it then crashes with a EXC_BAD_ACCESS.

I have stepped through with the debugger and determined that everything is correct up until the reloadData call. The database and main array are both synchronised and the custom TableView property is also pointing to the main array.

Then, as I step through with the debugger, I noticed a couple of things. Firstly, when it calls numberOfRowsInSection, the pointer to the main array is correct and the number of rows is correct. Secondly, when it gets to the first cellForRowAtIndexPath call, the pointer to the main array is pointing to an invalid section of memory. The actual memory address is the same but the array of objects has disappeared.

I cannot understand how the array disappears in the middle of the reload like this because my reference count should still be greater than zero so there should be no garbage collection. So my question is, does anybody have any idea what might be happening? And secondly, how do I determine the exact location of the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一萌ing 2024-12-27 01:48:32

几乎可以肯定您保留的数组不足。当你说“我的引用计数应该仍然大于零”时,几乎可以肯定不是。如果是的话,它就不会被释放。

首先寻找您直接访问 ivars 的任何地方。这是内存管理错误的第一大原因。只能通过访问器 (self.thing) 访问 ivars,dealloc 和 init 中除外。这将解决 90% 的此类问题。

You are almost certainly under-retaining the array. When you say "my reference count should still be greater than zero," it almost certainly is not. If it were, it would not have been released.

Start by looking for any place you directly access ivars. This is the #1 cause of memory management errors. Access ivars only through accessors (self.thing) except in dealloc and init. This will solve 90% of these kinds of problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文