Hey everyone. Based on the codeviewer sample, I tr...
# compose-ios
p
Hey everyone. Based on the codeviewer sample, I tried using a different font in my app. The font is updated on android but on iOS it causes the app to crash on startup. The font files are stored in commonMain/resources/font directory. The app is built on top of the compose-ios template. My commonMain contains the following expect definition:
Copy code
@Composable
expect fun font(res: String, weight: FontWeight, style: FontStyle = FontStyle.Normal): Font
And the iosMain actual implementation is:
Copy code
@OptIn(ExperimentalResourceApi::class)
@Composable
actual fun font(res: String, weight: FontWeight, style: FontStyle): Font {
    return cache.getOrPut(res) {
        val byteArray = runBlocking {
            resource("font/$res.ttf").readBytes()
        }
        Font(res, byteArray, weight, style)
    }
}
Error Log:
Copy code
Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: UNKNOWN_0xD at 0x0000000000000000
Exception Codes: 0x000000000000000d, 0x0000000000000000
VM Region Info: 0 is not in any region.  Bytes before following region: 4423217152
      REGION TYPE                    START - END         [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                      107a4f000-109ba7000    [ 33.3M] r-x/r-x SM=COW  ....app/MyApp
Termination Reason: SIGNAL 11 Segmentation fault: 11
Terminating Process: exc handler [54296]

Triggered by Thread:  0

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   MyApp                      	       0x108dbc62f IsInstance + 15
1   MyApp                      	       0x107cdd19c kfun:androidx.compose.runtime.CompositionImpl#composeContent(kotlin.Function2<androidx.compose.runtime.Composer,kotlin.Int,kotlin.Unit>){} + 1020
2   MyApp                      	       0x107d107d6 kfun:androidx.compose.runtime.Recomposer#composeInitial(androidx.compose.runtime.ControlledComposition;kotlin.Function2<androidx.compose.runtime.Composer,kotlin.Int,kotlin.Unit>){} + 1350
3   MyApp                      	       0x107cc29a9 kfun:androidx.compose.runtime.ComposerImpl.CompositionContextImpl.composeInitial#internal + 265
4   MyApp                      	       0x107cdc325 kfun:androidx.compose.runtime.CompositionImpl#setContent(kotlin.Function2<androidx.compose.runtime.Composer,kotlin.Int,kotlin.Unit>){} + 581
5   MyApp                      	       0x10814447a kfun:androidx.compose.ui.layout.LayoutNodeSubcompositionsState.subcomposeInto#internal + 762
6   MyApp                      	       0x108143f9d kfun:androidx.compose.ui.layout.LayoutNodeSubcompositionsState.subcompose#internal.158 + 1373
7   MyApp                      	       0x1081439b2 kfun:androidx.compose.ui.layout.LayoutNodeSubcompositionsState.subcompose#internal + 1554
8   MyApp                      	       0x10814317b kfun:androidx.compose.ui.layout.LayoutNodeSubcompositionsState#subcompose(kotlin.Any?;kotlin.Function2<androidx.compose.runtime.Composer,kotlin.Int,kotlin.Unit>){}kotlin.collections.List<androidx.compose.ui.layout.Measurable> + 3595
9   MyApp                      	       0x108149b65 kfun:androidx.compose.ui.layout.LayoutNodeSubcompositionsState.Scope.subcompose#internal + 149
10  MyApp                      	       0x108796a63 kfun:androidx.compose.material3.ScaffoldLayout$lambda$6$lambda$5#internal + 19603
The complete error log is quite long, so I have attached it below. Am I making a mistake somewhere or have things changed with v1.5.0.
a
Did it work before 1.5.0?
p
Haven't tried it on my end. But since it's working in the sample, I assumed that it was. With 1.5, there were some changes to the iOS resource management. Could that have caused something because of which the .ttf files are not accessible because when debugging I get
byteArray = [..., ..., ...,      , ...]
as the value after the readBytes operation.
a
Is it crashing while parsing the font?
p
No. Seems like font parsing is working correctly. On crash, debugger takes me to this line in the HashMap class of kotlin collections
Copy code
private fun hash(key: K) = if (key == null) 0 else (key.hashCode() * MAGIC) ushr hashShift
And I get this exception in the debugger:
Copy code
Exception = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
Before the actual implementation in iosMain I also create a mutable map called cache (as done in the sample as well)
Copy code
private val cache: MutableMap<String, Font> = mutableMapOf()
a
On what key is the hash being called?
Can you make a minimal reproducer project and submit a bug report?
p
This is the debug trace for the crash.
Copy code
kotlin.collections.HashMap.hash#internal HashMap.kt:218
HashMap#addKey HashMap.kt:294
HashSet#add HashSet.kt:37
kotlin.Throwable.ExceptionTraceBuilder.dumpSelfTrace#internal Throwable.kt:94
kotlin.Throwable.ExceptionTraceBuilder.dumpFullTrace#internal Throwable.kt:84
kotlin.Throwable.ExceptionTraceBuilder.print#internal Throwable.kt:71
Throwable#printStackTrace Throwable.kt:55
ReportUnhandledException RuntimeUtils.kt:153
(anonymous namespace)::terminateWithUnhandledException(ObjHeader*)::$_1::operator()() const 0x0000000103307e09
void (anonymous namespace)::$_0::operator()<(anonymous namespace)::terminateWithUnhandledException(ObjHeader*)::$_1>((anonymous namespace)::terminateWithUnhandledException(ObjHeader*)::$_1) 0x0000000103307cdf
(anonymous namespace)::terminateWithUnhandledException(ObjHeader*) 0x0000000103307af9
(anonymous namespace)::processUnhandledException(ObjHeader*) 0x0000000103307ab7
(anonymous namespace)::TerminateHandler::kotlinHandler() 0x00000001033077cc
std::__terminate(void (*)()) 0x00007ff800257b65
__cxa_rethrow 0x00007ff80025a3a4
objc_exception_rethrow 0x00007ff80004dd0d
CFRunLoopRunSpecific 0x00007ff800381769
GSEventRunModal 0x00007ff809cb128a
-[UIApplication _run] 0x0000000107434ad3
UIApplicationMain 0x00000001074399ef
<unknown> 0x000000010b23d667
<unknown> 0x000000010b23d514
<unknown> 0x000000010a8a17e9
$s8NetCloud6iOSAppV5$mainyyFZ iOSApp.swift:3
main iOSApp.swift:0
start_sim 0x00000001061782bf
start 0x000000010f87141f
The key is 0x7ff800031e47 if that helps.
a
I meant what object is the hash being calculated of?
What’s being added to the HashSet?
p
Nothing explicitly. Only to a map (key: String and value: Font)
a
Hey @Alexander Maryanovsky i got this error when parsing a font .. is there a workaround ?
c
Similar error here. Has anyone fix it?