swiftmacosapple-pdfkit

Getting EXC_BAD_ACCESS exception when accessing non null variable


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.sstatic.net/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


Solution

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