【发布时间】:2012-11-12 17:32:35
【问题描述】:
我正在实现一个简单的文件浏览器(在NSOutlineView 中)并且在扩展我的根节点时遇到了EXC_BAD_ACCESS。我的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
在展开根“文件”项后立即发生崩溃。
【问题讨论】:
标签: objective-c memory-management automatic-ref-counting exc-bad-access nsoutlineview