0

I got a EXC_BAD_ACCESS error in this code at the end where the constant "line" is declared. At the line that says "let line: CTLine = CTLineCreateWithAttributedString(attrString)". The exact error says "Thread 12: EXC_BAD_ACCESS (code=1, address=0x7e8)".

Can I get some help identifying the problem?

I also see in the answers to similar questions something called "Zombie" to help in debugging. What is Zombie and how do I turn that on?

import UIKit
import MapKit
import CoreText

class LabelOverlayRenderer: MKOverlayRenderer {

    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {

//        let labelOverlay = overlay as! LabelOverlay

        let string: CFString
        let font: CTFont

        let nsString = NSString(string: "Hello" as CFString)
        string = nsString as CFString

        font = CTFontCreateUIFontForLanguage(.label, 17, "US" as CFString)!

        var keys: [CFString] = [kCTFontAttributeName]
        var values: [CFTypeRef] = [font]

        var mutablePointerKeys: UnsafeMutablePointer<UnsafeRawPointer?>!
        var rawPointerKeys: UnsafeRawPointer?

        rawPointerKeys = UnsafeRawPointer?(&keys)
        mutablePointerKeys = UnsafeMutablePointer<UnsafeRawPointer?>!(&rawPointerKeys)

        var mutablePointerValues: UnsafeMutablePointer<UnsafeRawPointer?>!
        var rawPointerValues: UnsafeRawPointer?

        rawPointerValues = UnsafeRawPointer?(&values)
        mutablePointerValues = UnsafeMutablePointer<UnsafeRawPointer?>!(&rawPointerValues)

        var cfTypeDictionaryKeyCallBacks = CFDictionaryKeyCallBacks()
        var cfTypeDictionaryValueCallBacks = CFDictionaryValueCallBacks()

        let attributes: CFDictionary = CFDictionaryCreate(kCFAllocatorDefault, mutablePointerKeys, mutablePointerValues, MemoryLayout.size(ofValue: keys)/MemoryLayout.size(ofValue: keys[0]), &cfTypeDictionaryKeyCallBacks, &cfTypeDictionaryValueCallBacks)

        let attrString: CFAttributedString = CFAttributedStringCreate(kCFAllocatorDefault, string, attributes)

        let line: CTLine = CTLineCreateWithAttributedString(attrString) // * Error occurs here. It says "Thread 12: EXC_BAD_ACCESS (code=1, address=0x7e8)". 

        context.textPosition = CGPoint(x: 10, y: 10)
        CTLineDraw(line, context)

    }
}
daniel
  • 1,446
  • 3
  • 29
  • 65

1 Answers1

0

When you want to access a variable that is currently de allocated , you will have EXC_BAD_ACCESS , with Zombie enabled you can see that variable , see this to enable it nszombie

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87