2012-04-19 72 views
0

後我有一個是顯示分層HTML數據,從一個HTML解析器加載一個NSOutlineTableView(每個HTML標籤是與屬性的對象和它的一個陣列的兒童)。出於某種原因,此代碼在所有對象運行後崩潰。也就是說,我看到的NSLog()結果在HTML中的所有對象,但在這之後的代碼崩潰:NSOutlineTableView崩潰最後元件

- (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內容: @"<ul></ul><span class='spantext1'></span><span class='spantext2'></span>";

輸出:

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) 

這裏是調用堆棧的頂部:

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) 

它幾乎看起來好像它試圖運行objectValueForTableColumn後,所有的數據已經運行通過。就像我說的那樣,崩潰發生在整個對象層次結構運行之後(如終端輸出所證明的那樣)。無論如何,在崩潰之前,tableView會在屏幕上出現一瞬間,所有三個元素都在它們各自的行中。

回答

1

EXC_BAD_ACCESS」通常意味着你試圖訪問一個零對象或指針。

,從我可以在你的代碼中看到在那裏,它看起來像你總是假設「[item children]」不是零。

如果「[item children]」沒有孩子會發生什麼?結果是NULL嗎?你應該在NULL對象上調用objectAtIndex還是count?我的猜測是沒有。

你或許應該返回一些明智的每個功能,如果沒有孩子任何給定項。

+0

[item children]始終不爲NULL,如果該項目沒有子項,則該數組僅爲空。無論如何,在[item children]的調用中不會發生崩潰,而是在顯示最後一個元素(在這種情況下,[item isKindOfClass:]。)上的最後一次調用是objc_msgSend_vtable4,如果這意味着什麼對任何人...... – Chris 2012-04-20 00:06:31

+0

感謝添加itty片斷回溯。是項有效或者是在該行「'nil'」之前它崩潰?另外,您使用ARC又是怎樣的項目數據在你的代碼中引用(即任何機會的數據將失效或在崩潰之前發佈)? – 2012-04-20 00:14:06

+0

沒問題。讓我們看看.. ARC是關閉的。該項目不評估爲零或NULL,但地址xCode爲我提供了在解析時從來沒有見過的壞對象,這些對象都是在HTML解析器中創建的,我從互聯網上得到這是一個圍繞libxml/HTMLparser的Cocoa包裝器,所以我沒有觸及那裏的內存管理。有沒有可能這只是解析器中的一個錯誤? – Chris 2012-04-20 00:24:53