How can I pass `UnsafeMutablePointer<CGGlyph&gt...
# kotlin-native
l
How can I pass
UnsafeMutablePointer<CGGlyph>
to a C function? Currently I have this. It compiles but the code doens't work:
Copy code
val foo: CPointer<UShortVarOf<CGGlyph>> = memScoped {
  createValues<CGGlyphVar>(2) { CGGlyph.MIN_VALUE }.ptr
}
l
Any memory allocated inside a memscoped becomes invalid when the lambda returns. Make sure to do any work that requires the pointer inside the lambda, or use an arena to allocate if the pointer needs to stay valid.
As a general rule, never let a pointer leave a memScoped block.
l
Yeah I should have pasted the real code, but AFAIK I should be safe 🙏 it really is something akin to:
Copy code
fun hasGlyph(emoji: String): Boolean = memScoped {
  ...
  val glyphs = createValues<CGGlyphVar>(2) { CGGlyph.MIN_VALUE }.ptr
  return@memScoped CTFontGetGlyphsForCharacters(font, uniChars, glyphs, uniChars.size.toLong())
}
l
That snippet looks to be handling memory right. Is
uniChars.size
<= 2? Have you verified that uniChars is correct?
I would assume "doesn't work" in this case means it always returns false? Have you validated that the uniChars input is valid, since that should be what affects functionality here. You don't seem to need the glyphs array, so I wonder what happens if you provide null? The docs don't clarify this.
l
That's right, or almost, it always returns true. On StackOverflow this is used to figure whether an emoji can be rendered, but on my case I know that some must return false (testing on an older iOS version). I still couldn't figure it out, but I found out that
glyphs
gets populated if it really can be rendered by the OS. That is, I get what I want if I don't check for the return type of
CTFontGetGlyphsForCharacters
, but rather from `glyph`s length...