3

Question

I upgraded my project to Swift 3.0, but after the update the app won't run on the simulator. It doesn't crash, but simply freeze.

I investigated the issue and saw the the apps hangs on the initialisation of the custom fonts.

I copied the function below.

func registerMaterialFont() {
    let url = Bundle.main.url(forResource: "Material-Design-Iconic-Font", withExtension: "otf")!
    let data = try! Data(contentsOf: url)

    let provider = CGDataProvider(data: data as CFData)

    print("Test 1")

    let font = CGFont(provider!)

    print("Test 2")

    var error: Unmanaged<CFError>?
    if !CTFontManagerRegisterGraphicsFont(font, &error) {
        //Error handling here
    } else {
        log.info("Material Font registered");
    }
}

The console will only print Test 1.

Does somebody has an idea whats going on?

NB:. This app is running on a testPhone with sim card and not running on an phone without a simcard!

Edit: Stack trace If I press pauze in the Debug console, the following stack trace is displayed:

Stack Trace

libsystem_kernel.dylib`semaphore_wait_trap:
    0x10c765fa4 <+0>:  movq   %rcx, %r10
    0x10c765fa7 <+3>:  movl   $0x1000024, %eax          ; imm = 0x1000024 
    0x10c765fac <+8>:  syscall 
->  0x10c765fae <+10>: retq   
    0x10c765faf <+11>: nop    
Bas
  • 780
  • 5
  • 17
  • 2
    Run the app through the debugger. When it gets stuck, click on the Pause button in the debugger and look at the resulting stack trace to see where it is stuck. – rmaddy Oct 25 '16 at 14:25
  • @rmaddy I updated the stack trace to the comment. – Bas Oct 25 '16 at 14:32
  • Looks like a nasty bug in `CGFontCreate`. It's using `dispatch_once` which in turn is calling `CGFontCreate`. The 2nd call is now blocked waiting on the previous call to finish. – rmaddy Oct 25 '16 at 14:35
  • Curious why you are loading the font using all of this code? Why not register your otf file the "normal" way of adding custom fonts? See http://stackoverflow.com/questions/360751/can-i-embed-a-custom-font-in-an-iphone-application – rmaddy Oct 25 '16 at 14:45
  • Actually this are icon sets. (material design and font-awesome). I am calling the function in app delegate (maybe this isn't allowed), http://www.openradar.me/18778790 – Bas Oct 25 '16 at 14:50
  • 1
    See the note in the rdar you linked? *"Calling [UIFont systemFontOfSize:12]; before drawing PDF fixes the problem."*. Try that yourself. Access `UIFont` at the start of your `registerMaterialFont` method. – rmaddy Oct 25 '16 at 14:53
  • @rmaddy: Thanks! _ = UIFont() – Bas Oct 25 '16 at 15:24
  • 1
    Instead of posting that as an update to your question, post a proper answer. – rmaddy Oct 25 '16 at 15:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126639/discussion-between-bas-and-rmaddy). – Bas Oct 25 '16 at 15:28

1 Answers1

9

This is a bug in IOS/Swift. (See for more information: http://www.openradar.me/18778790)

Calling

_ = UIFont() 

before

CGFontCreate()

Will prevent the function from deadlocking.

Bas
  • 780
  • 5
  • 17
  • Calling `[UIFont systemFontOfSize:12];` as suggested in the link worked for me. – Adamski Feb 06 '17 at 15:36
  • Any idea what is the reason for this ?? I had the same issue trying to create let fontRef = CGFont(fontProviderRef) and the App Crashes. When I added let _ = UIFont() It's fixed and the crash doesn't happen, but I have no idea why? – KarimIhab Feb 05 '18 at 15:15