Piasy
01/29/2020, 9:41 AMtypedef const char* (*PCClientOnPreferCodecs)(void*, const char*, const char*);
And my Kotlin code implementing it is:
private fun PCClientCallbackOnPreferCodecs(opaque: COpaquePointer?, peerUidPtr: CPointer<ByteVar>?, sdpPtr: CPointer<ByteVar>?): CPointer<ByteVar>? {
initRuntimeIfNeeded()
if (opaque == null) {
return null
}
memScoped {
return opaque.asStableRef<PeerConnectionClientCallback>().get().onPreferCodecs(peerUid, sdp).cstr.ptr
}
}
I pass it into C++ with staticCFunction(::PCClientCallbackOnPreferCodecs)
, and pass opaque
with StableRef.create(callback).asCPointer()
.
It could be compiled and linked, but at runtime the string I get at C++ is scrambled code.Artyom Degtyarev [JB]
01/29/2020, 9:47 AMmemScoped
block here. Pointers allocated like that become invalid as soon as you get out of the block.Piasy
01/29/2020, 9:48 AMmemScoped
, how could I convert String
to CPointer<ByteVar>
?Artyom Degtyarev [JB]
01/29/2020, 9:53 AMPiasy
01/29/2020, 9:54 AMprivate fun PCClientCallbackOnPreferCodecs(opaque: COpaquePointer?, peerUidPtr: CPointer<ByteVar>?, sdpPtr: CPointer<ByteVar>?): CPointer<ByteVar>? {
initRuntimeIfNeeded()
if (opaque == null) {
return null
}
return opaque.asStableRef<PeerConnectionClientCallback>().get().onPreferCodecs(peerUid, sdp).cstr.getPointer(nativeHeap)
}
But it couldn't compile, it fails with Type mismatch: inferred type is nativeHeap but AutofreeScope was expected
.msink
01/29/2020, 10:17 AMPiasy
01/29/2020, 10:34 AMconst char*
.msink
01/29/2020, 10:37 AMPiasy
01/29/2020, 11:05 AMmsink
01/29/2020, 11:34 AM.cstr.getPointer(nativeHeap)
is broken, I use .cstr.place(nativeHeap.allocArray(buflen))
instead.
But you have to somehow calculate or guess buflen
here.
And somewhere later call nativeHeap.free(it)
, or else you will have memory leak.Piasy
01/29/2020, 11:36 AMmsink
01/29/2020, 11:42 AMPiasy
01/29/2020, 11:43 AM