【问题标题】:"-[CFString respondsToSelector:]: message sent to deallocated instance" when trying to access objectArray“-[CFString respondsToSelector:]: message sent to deallocated instance”尝试访问 objectArray 时
【发布时间】:2011-07-02 11:53:43
【问题描述】:

当程序获取最后一行时,我正在尝试在 tableView 中加载接下来的 10 行数据

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

我正在使用带有 pagenumber 参数的单个函数来加载连续页面。 它适用于第一次获取,即页码 0,但是当我再次调用它来获取下一页行时,它给了我这个错误

-[CFString respondsToSelector:]: message sent to deallocated instance

敲了很长时间后,我发现它在尝试访问我的模型对象的 NSMutableArray 时会产生这个问题。 我就是这样用的:

TableRowObjectClass* TempObject = [[TableRowObject alloc] init];
for(int i=0;i<rowArray.count;i++){
    TempObject = [rowArray objectAtIndex:i];
    NSString* objectProperty = [[[NSString alloc] initWithFormat:@"String Property I need from Object to be appended with some text from TableRowObject Class : %@",TempObject.objProperty] retain];
    [propertyArray addObject:objectProperty];
    [objectProperty release];
}

在这里,我得到了模型对象数组(TableRowObjectClass 的对象),我想从 TempObject 中提取 objectProperty 并创建所有这些属性的数组。 最初,对于pagenumber 0,它获取 20 行,现在当我显示第 20 行时,我调用 fetch,它调用此函数来创建 objectProperty 的新数组,我调用 [TableView reloadData] 以显示带有 20 的 frsh 提要(旧)+20(新鲜)Feed。

现在它创建了这个错误

-[CFString respondsToSelector:]: message sent to deallocated instance

尝试访问时,

NSString* objectProperty = [[[NSString alloc] initWithFormat:@"String Property ....TableRowObject Class: %@",TempObject.objProperty] retain];

我不确定dealloc 是什么以及在哪里得到的。 我在这方面花了很多时间,但我对 Objective-C 内存管理并不是很擅长。

非常感谢您的帮助。

【问题讨论】:

  • TableRowObject 是类还是变量?你似乎同时使用它。
  • 它是一个有自己的 init 方法的类。它的对象是通过解析 JSON 数据创建的,并在这些对象的 NSMutable 数组中返回。
  • 感谢您指出这一点,我在上面的帖子中修复了它。这不是实际的代码。 (无法粘贴确切的代码,公司政策)我正在尝试展示功能,这与现在上面提供的代码类似。

标签: iphone objective-c uitableview memory-management memory-leaks


【解决方案1】:

您的代码非常清楚地表明您不了解 Objective-C 或 Objective-C 内存管理;不用担心,我们都从那里开始。首先阅读Objective-C guide,然后阅读Memory Management guide

一些具体问题:

TableRowObjectClass* TempObject = [[TableRowObject alloc] init];
TempObject = [someArray objectAtIndex: 0];

那泄漏了。在第一行分配的对象(变量也应该以小写字母开头)。

这个:

NSString* objectProperty = [[[NSString alloc] initWithFormat:@"..."] retain];
[objectProperty release];

一个双重保留,一个释放,另一个泄漏。

您看到message sent to deallocated instance 错误表明您正在过度释放其他对象。遵循内存管理指南,使用“构建和分析”,然后使用 Zombie 检测工具....

【讨论】:

  • 感谢您的建议。我遇到了另一个问题,我正在使用一个类方法并返回一个字典。但是当我尝试将其初始化为自动释放时,应用程序崩溃,因为它正在另一个类方法中使用
  • 从类方法或实例方法返回的对象对于内存管理而言完全没有区别......
【解决方案2】:

没有必要使用

NSString* objectProperty = [[[NSString alloc] initWithFormat:@"String Property ....TableRowObject : %@",TableRowObject.objProperty] retain];

你甚至可以使用

NSString* objectProperty = [NSString stringWithFormat:@"String Property ....TableRowObject : %@",TableRowObject.objProperty];

【讨论】:

  • 我最初只使用它 (NSString* objectProperty = [NSString stringWithFormat:@"String Property ....TableRowObject : %@",TableRowObject.objProperty];) ,但急于解决这个问题错误,而不是自动释放任何对象,我使用了它 (NSString* objectProperty = [[[NSString alloc] initWithFormat:@"String Property ....TableRowObject : %@",TableRowObject.objProperty] retain];) .
  • TableRowObject 中有哪些对象
  • 这个类是我自己定义的,它是继承自 NSObject 的常规类。
猜你喜欢
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-11
  • 2013-07-03
  • 2012-11-17
相关资源
最近更新 更多