Search code examples
swiftcocoaxcode7nsoutlineview

NSOutlineView EXC_BAD_ACCESS crash on expand


I am trying to use an NSOutlineView in my project, using Swift. I have done this a few times already using Objective-C without any problem, but for some reason now my app keeps crashing with EXC_BAD_ACCESS, mostly when trying to expand a cell.

I have created a new test project with only an outline view and the 4 data source methods, but the crashes happen, there, too.

Here is the minimal implementation:

func outlineView(outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) -> Int {

    return 3
}

func outlineView(outlineView: NSOutlineView, child index: Int, ofItem item: AnyObject?) -> AnyObject {

    return "Test"
}

func outlineView(outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool {

    return true
}

func outlineView(outlineView: NSOutlineView, objectValueForTableColumn tableColumn: NSTableColumn?, byItem item: AnyObject?) -> AnyObject? {

    return nil
}

Any idea of what I am doing wrong?

I have uploaded the test project here. Please review it. https://drive.google.com/file/d/0BzEhecUbyNeFS3JGN1V0SlJ0dWM/view


Solution

  • There are a few things I had to fix to make your sample work as expected:

    • There's a crash related to "Test" being a local string and released before the outline view tries to retain it. This is fixed by having the items owned by the view controller.

    • After that, I encountered a crash due to infinite recursion. That was fixed by having a data model instead of telling the outline view that every item had 3 children no matter what level the item was at.

    • I also changed the items to be instances subclassed from NSObject rather than String because I remember reading that that was necessary. (I currently can't find the reference.)