0

As you can see in the linked image, I am getting:

EXC_BAD_ACCESS (code=1, address=0x0)

when accessing outline.numberOfChildren but lldb shows that outline is not nil and that outline.numberOfChildren is 0 (which is exactly what it should be in this case). Why is this happening? Thanks.

link to image: https://i.stack.imgur.com/quzUC.jpg

Code:

 func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool { 
  if self.rootOutline != nil {
     if let outline = item as? PDFOutline {
         if outline.numberOfChildren == 0 {  // <- Error here
            return false
          }
         return true
       }
     if self.rootOutline!.numberOfChildren == 0 {
       return false
       }
     return true
    }
  return false
 }

link to Xcode project on github: https://github.com/raphaelreyna/Chapters

rareyna
  • 141
  • 9

1 Answers1

0

The outline is lazily loaded and can't load if the PDFDocument is released from memory. Solution: keep a strong reference to the PDFDocument.

Willeke
  • 14,578
  • 4
  • 19
  • 47
  • But isn't `if let outline = item as? PDFOutline` creating the strong reference you mention? – Cristik Mar 08 '19 at 05:05
  • This worked, thanks Willeke. I never would have guessed that this was the issue. – rareyna Mar 08 '19 at 06:02
  • @Cristik `item` is a strong reference to a `PDFOutline` but the `PDFOutline` items don't keep a strong reference to the document, `document` is a weak property of `PDFOutline`. `outlineRoot` is a strong property of `PDFDocument`. Creating a strong reference in `outlineView(_:isItemExpandable:)` wouldn't work because the document is already released. – Willeke Mar 08 '19 at 11:34