https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
p

Piasy

01/27/2020, 12:19 PM
Hey guys, I'm making Kotlin Multiplatform work on Windows now, but I have an issue of calling Kotlin back from C++. Here is my use case: I want use socket.io-client-cpp to connect to a SocketIO server, and get a callback when it connects successfully. But the callback is not invoked on main thread, I do notice the note on the C Interop page, calling
kotlin.native.initRuntimeIfNeeded()
in my kotlin code before I connect to server, but it still crash my program. If I fire the callback on main thread, e.g. APP C++ code calls
kotlinTestFunc
, which calls
cppSocketIOConnect
, and fire the callback directly inside
cppSocketIOConnect
, it works fine. Do I miss something?
👀 1
a

Artyom Degtyarev [JB]

01/27/2020, 12:28 PM
Can you share more details on the error?
p

Piasy

01/27/2020, 12:37 PM
Sorry, visual studio just says Exception thrown:
Copy code
Exception thrown at 0x004FEFB0 in WindowsExample.exe: 0xC0000005: Access violation executing location 0x004FEFB0.
I write it to crash dump and analyze it with dbghelper, but the stack also doesn't give helpful message, the attachment is the full analyze output.
Sorry the previous attachment may contain duplicate content, please check this one, thanks!
Oh sorry @Artyom Degtyarev [JB], I find the reason! It turns out that I do the wrong capture in C++ lambda, how stupid am I! Here is the working code snippet:
Copy code
void SioOn(void* sio, SIOListener2 listener) {
    reinterpret_cast<sio::client*>(sio)->set_socket_open_listener(
        [=](std::string const& nsp) {
            listener();
    });
}
Previously I capture it by reference
[&]
.
🎉 1
Oh btw, do you know how to create
.pdb
when compile kotlin code to
.dll
on windows? @Artyom Degtyarev [JB]
a

Artyom Degtyarev [JB]

01/27/2020, 1:40 PM
Seems like there is no such option for now. The best you can get here is a dwarf-compatible debug info, see here(https://github.com/JetBrains/kotlin-native/blob/master/DEBUGGING.md).
p

Piasy

01/27/2020, 1:41 PM
Thanks! I'll check it out.
Hi @Artyom Degtyarev [JB], I have a follow up question in the non-main thread case: I want to pass some data when the callback is fired in C++ code, e.g. a integer, but if I access the
Int
in kotlin code, my program crashed with error:
Exception thrown at 0x6514A0E9 (AvConf.dll) in WindowsExample.exe: 0xC0000005: Access violation reading location 0x00000020.
if I don't access the integer, it works fine, if I fire the callback directly in main thread, it also works fine.
a

Artyom Degtyarev [JB]

01/27/2020, 2:15 PM
I’m sorry, but I’m missing something here. Are you sending a Kotlin’s
StableRef
to the callback or it is a C++
int
that the Kotlin function takes as input?
p

Piasy

01/27/2020, 2:28 PM
It is a C++
int
that the Kotlin function takes as input. Here is my C++ code:
Copy code
typedef void(*SIOListener)(int);
void SioOn(void* sio, const char* event, SIOListener listener) {
    reinterpret_cast<sio::client*>(sio)->set_socket_open_listener(
        [=](std::string const& nsp) {
        listener(0);
    });
}
Here is my kotlin code:
Copy code
private fun SioListenerFunction2(len: Int) {
  Logging.error("SIO", "SioListenerFunction2")
  Logging.error("SIO", "len $len")
}

NKBundle.SioOn(socket, event, staticCFunction(::SioListenerFunction2))
If I don't log
len
param, or if I invoke
listener
directly, it works fine.
btw, the
socket
variable is created by a C++ function, so don't worry about it.
a

Artyom Degtyarev [JB]

01/27/2020, 2:42 PM
Try to call
initRuntimeIfNeeded()
here, before logging.
Also, I got some suspicions about passing value like that, maybe one should create a parameter with default value like
Copy code
[=](std::string const& nsp, int zero = 0) {
        listener(zero);
    }
, but it’s just a guess.
p

Piasy

01/27/2020, 3:00 PM
Hi @Artyom Degtyarev [JB], calling
initRuntimeIfNeeded()
does the magic! So the note in C Interop page means it should be called inside the callback right? And regarding your comment on C++ code, actually passing 0 back is only for test purpose, there shall be more data from C++, and the
int
is the length of an array. Thank you very much!
👌 1
4 Views