我正在实现一个简单的文件浏览器(在 an 中NSOutlineView
),并且EXC_BAD_ACCESS
在扩展我的根节点时遇到了 an 。我NSOutlineViewDataSource
返回的孩子如下:
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
if (!item) {
// Root node
return @"Files";
}
NSFileManager *manager = [NSFileManager defaultManager];
NSError *error = nil;
return [[manager contentsOfDirectoryAtPath:@"/" error:&error] objectAtIndex:index];
}
从这个方法返回的NSString
正在被自动释放,当 AppKit 代码在这里调用时:
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item {
NSString *identifier = [item isEqualToString:@"Files"] ? @"HeaderCell" : @"DataCell";
NSTableCellView *cell = [outlineView makeViewWithIdentifier:identifier owner:self];
cell.textField.stringValue = item;
return cell;
}
item
已经没了。这是项目生命周期的 Instruments 屏幕截图:
我不确定我做错了什么 - 我不能明确地做retain
任何事情(我也不应该!)因为 ARC 已启用,但子项仍然丢失。
编辑:实际崩溃的堆栈跟踪:
0 CoreFoundation -[__NSCFString retain]
1 Spark -[SPFileBrowserController outlineView:child:ofItem:] /Users/Craig/projects/Spark/Spark/SPFileBrowserController.m:29
2 AppKit loadItemEntryLazyInfoIfNecessary
3 AppKit -[NSOutlineView _rowEntryForChild:ofParent:requiredRowEntryLoadMask:]
4 AppKit -[NSOutlineView _expandItemEntryChildren:atStartLevel:expandChildren:andInvalidate:]
5 AppKit -[NSOutlineView _expandItemEntry:expandChildren:startLevel:]
6 AppKit -[NSOutlineView _batchExpandItemsWithItemEntries:expandChildren:]
7 AppKit -[NSOutlineView expandItem:expandChildren:]
8 AppKit -[NSOutlineView _doUserExpandOrCollapseOfItem:isExpand:optionKeyWasDown:]
9 AppKit -[NSOutlineView _outlineControlClicked:]
10 AppKit -[NSApplication sendAction:to:from:]
11 AppKit -[NSControl sendAction:to:]
12 AppKit -[NSCell _sendActionFrom:]
13 AppKit -[NSCell trackMouse:inRect:ofView:untilMouseUp:]
14 AppKit -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:]
15 AppKit -[NSControl mouseDown:]
16 AppKit -[NSWindow sendEvent:]
17 AppKit -[NSApplication sendEvent:]
18 AppKit -[NSApplication run]
19 AppKit NSApplicationMain
20 libdyld.dylib start
编辑 2:项目现已附加。编辑 的返回值SPFileUtil
childrenOfFolder:
以使用选项 2 以一致地重现。使用单个时它总是通过,但在使用内容NSString
时总是失败。NSFileManager
https://www.dropbox.com/s/lqj5r5ndg0qusak/Spark.zip
在展开根“文件”项后立即发生崩溃。