2012-04-19 3 views
0

HTML 파서에서로드 된 계층 적 HTML 데이터를 표시하는 NSOutlineTableView가 있습니다 (각 HTML 태그는 특성을 가진 개체이며 자식 배열입니다). 어떤 이유로이 코드는 모든 객체를 실행 한 후에 충돌합니다. 그건 내가 HTML에있는 모든 개체에 대한 NSLog() 결과를보고, 말을하는 것입니다 만, 그 후 코드 충돌 : HTML 내용 : @"<ul></ul><span class='spantext1'></span><span class='spantext2'></span>";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]]; 

} 

는 예를 들어 실행은 다음과 같습니다

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) 
: 여기
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을 다시 한 번 실행하려고하는 것처럼 보입니다. 내가 말했듯이, 충돌은 전체 객체 계층 구조가 실행 된 후에 발생합니다 (터미널 출력에 의해 입증 됨). 여하튼 크래시가 발생하기 전에 테이블 뷰가 화면에 두 번째로 나타나며 각 행에 세 요소가 모두 표시됩니다.

답변

1

"EXC_BAD_ACCESS"은 대개 무 객체 또는 포인터에 액세스하려고한다는 것을 의미합니다.

그리고 코드에서 볼 수 있듯이 항상 "[item children]"이 0이 아닌 것으로 가정합니다.

"[item children]"에 자녀가 없을 경우 어떻게됩니까? 결과가 NULL인가? NULL 객체에서 objectAtIndex 또는 count을 호출해야합니까? 내 추측은 아니오입니다.

주어진 항목에 대해 하위 항목이 없으면 각 기능에 대해 현명한 것을 돌려 주어야합니다.

+0

[item children]은 항상 NULL이 아니며 항목에 자식이 없으면 배열은 단순히 비어 있습니다. 여하튼, [item children]에 대한 호출에서는 충돌이 발생하지 않고 마지막 요소가 표시된 후 항목에 대한 참조가 발생합니다 (이 경우 [item isKindOfClass :]). 마지막 스택 호출은 objc_msgSend_vtable4, 누군가에게 아무 의미가 있다면 ... – Chris

+0

itty bitty backtrace를 추가해 주셔서 감사합니다. 충돌이 발생하기 전에 항목이 유효한지 또는 "nil"입니까? 또한 ARC를 사용하고 있으며 항목 데이터는 어떻게됩니까? (즉, 데이터가 오래되거나 충돌 직전에 석방 될 가능성) –

+0

ARC가 꺼져 있습니다. 항목이 nil 또는 NULL로 평가되지 않지만 그 주소는 xCode는 구문 분석시 이전에 본 적이없는 잘못된 객체를 제공합니다. 개체는 모두 인터넷에서 내려받은 HTML 파서에서 생성됩니다.이 파서는 libxml/HTMLparser를 감싸고있는 Cocoa 래퍼이므로 메모리 관리를 건드리지 않았습니다. 파서에 버그가있을 가능성이 있습니까? – Chris

관련 문제