Macos NSOutlineTableView在最后一个元素之后崩溃

Macos NSOutlineTableView在最后一个元素之后崩溃,macos,cocoa,exc-bad-access,nstableview,nsoutlineview,Macos,Cocoa,Exc Bad Access,Nstableview,Nsoutlineview,我有一个NSOutlineTableView,它显示从HTML解析器加载的分层HTML数据(每个HTML标记都是一个具有属性的对象及其子对象数组)。由于某些原因,此代码在运行完所有对象后崩溃。也就是说,我在HTML中看到所有对象的NSLog()结果,但之后代码崩溃: - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item{ if (item==nil) {

我有一个NSOutlineTableView,它显示从HTML解析器加载的分层HTML数据(每个HTML标记都是一个具有属性的对象及其子对象数组)。由于某些原因,此代码在运行完所有对象后崩溃。也就是说,我在HTML中看到所有对象的NSLog()结果,但之后代码崩溃:

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item{

    if (item==nil) {
        item=rootNode;
    }
    NSLog(@"looking for child %d of element %@",index, item);
    return [[item children] objectAtIndex:index];

}


- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item{

    if (item==nil) {
        item=rootNode;
    }
    NSLog(@"Element %@ has %i children", item, [[item children] count]);
    return [[item children ]count];

}


- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item{

    NSLog(@"is item %@ with %@ children expandable?",item,[item children]);
    return [[item children] count]>0;

}


- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item{

    if (![item isKindOfClass:[HTMLNode class]]) {   //////CRASH ON THIS LINE (or at least in this function)
        return @"";
    }

    NSLog(@"Object value for: %@",item);

    return [NSString stringWithFormat:@"%@",[item description]];

}
下面是一个运行示例: HTML内容:
@“

    输出:

    Element <HTMLNode: 0x7fb9234462a0> body has 3 children
    
    looking for child 0 of element <HTMLNode: 0x7fb9234462a0> body
    
    is item <HTMLNode: 0x7fb923437d40> ul with (
    ) children expandable?
    
    Object value for: <HTMLNode: 0x7fb923437d40> ul
    
    looking for child 1 of element <HTMLNode: 0x7fb9234462a0> body
    
    is item <HTMLNode: 0x7fb9249255c0> span with (
    ) children expandable?
    
    Object value for: <HTMLNode: 0x7fb9249255c0> span
    
    looking for child 2 of element <HTMLNode: 0x7fb9234462a0> body
    
    is item <HTMLNode: 0x7fb92491e1d0> span with (
    ) children expandable?
    
    Object value for: <HTMLNode: 0x7fb92491e1d0> span
    (lldb) (crash with EXC_BAD_ACCESS)
    
    在所有数据运行完毕后,它几乎看起来好像要再次运行objectValueForTableColumn。正如我所说,崩溃发生在整个对象层次结构运行完成之后(由终端输出证明)。无论如何,在崩溃之前,tableView会在屏幕上出现一瞬间,所有三个元素都在各自的行中。

    EXC\u BAD\u ACCESS
    ”通常表示您正在尝试访问一个nil对象或指针

    从我在上面的代码中看到的情况来看,您似乎总是假设“
    [item children]
    ”不是零

    如果“
    [item children]
    ”没有子项,会发生什么情况?结果是否为空?您应该对空对象调用
    objectAtIndex
    还是
    count
    ?我猜不是


    如果任何给定项没有子项,您可能应该为每个函数返回一些合理的值。

    [item children]始终不为NULL,如果项没有子项,则数组为空。无论如何,崩溃并不是发生在对[item children]的调用上,而是发生在显示最后一个元素后对item的任何引用上(在本例中,[item isKindOfClass:)。堆栈上的最后一个调用是objc_msgSend_vtable4,如果这对任何人都有意义的话……感谢您添加了itty bitty回溯。item是否有效在崩溃前的行中出现“
    nil
    ”?此外,您是否正在使用ARC以及代码中引用的项目数据是如何使用的(即数据在崩溃前过时或被释放的可能性)?没问题。让我看看。.ARC已关闭。该项的计算结果不为零或NULL,但是xCode为坏对象提供的地址在解析过程中从未见过。这些对象都是在我从internet上获得的HTML解析器中创建的,它是libxml/HTMLparser的Cocoa包装器,因此我没有触及那里的内存管理。我有没有可能只是语法分析器中的一个bug?回答了我自己的问题,有点像。结果它似乎是HTML语法分析器代码中的一个bug,所以我去买了其他语法分析器,找到了一个工作完美的语法分析器。好吧,非常感谢你的帮助!哈哈,我真希望我能早点想到这一点,我似乎需要停止从语法分析器中信任库燕鸥笑。^^
    objc_msgSend() selector name: isKindOfClass:
    objc[23702]: garbage collection is OFF
    
    Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
    0   libobjc.A.dylib                 0x00007fff846e7110 objc_msgSend_vtable4 + 16
    1   com.303DesignLabs.SiteStats     0x0000000109dd44fd -[HTMLTreeOutlineController outlineView:objectValueForTableColumn:byItem:] + 189 (HTMLTreeOutlineController.m:51)