Could anyone help me? I write native library. I ha...
# kotlin-native
z
Could anyone help me? I write native library. I have a header with struct, which has follow line:
Copy code
PstFile (*PstFile)(const char* filePath, const char* mode);
When I call this constructor in C code I pass 2 string:
Copy code
PstFile.PstFile("/home/evgeni/Projects/SAGO/NLC/source.pst", "rb");
But in my library I get another string:
@&�9�
and
rb
if I swap the parameters, I get another string:
@���~
and
/home/evgeni/Projects/SAGO/NLC/source.pst
. Why first parameter anyhow?
a
Hello! Can you provide some more code to reproduce this error?
z
Kotlin native class:
Copy code
class PstFile(
    filePath: String, //(failed string)
    mode: String //(normal string)
) : Closeable {
    init {
        println(filePath)
        println(mode)
    }
...
    override fun close() {}
}
C code:
Copy code
typedef libpst_kref_com_tullynore_pst_PstFile PstFile;

int main() {
    libpst_ExportedSymbols *lib = libpst_symbols();

    const char *filePath = "/home/evgeni/Projects/SAGO/NLC/source.pst";
    const char *mode = "rb";


    PstFile pstFile = lib->kotlin.root.com.tullynore.pst.PstFile.PstFile(filePath,mode);
    const char *storeName = lib->kotlin.root.com.tullynore.pst.PstFile.getMessageStoreName(pstFile);
    printf("%s\n", storeName);

    lib->DisposeStablePointer(pstFile.pinned);
    return 0;
}
Output:
rb
@�A��
👀 1
This result was in 1.3.31 version. On the last version 1.3.40 I cannot get result at all.
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
I removed all
internal
modifiers and my library run. But it generates a lot of redundant information on the header.
a
After some research, let me ask something else. I’ve seen youtrack issue, but nothing happens when all the mentioned steps were made. The only way I came up with a reproduction of something like your problem was this: 1. everything works with no
internal
, just as you spotted 2. if I add
internal
modifier to the code, and then recompile library, my OLD C-written program starts to print random symbols. 3. But, when I recompile C program, this issue is gone! So, it would be nice if you can give me more info about the original project, or some non-trivial steps that you made. Currently, it looks more like an occasional mistake rather than K/N bug.
z
I have a file, which has only extension functions:
Copy code
internal fun Byte.toUByte(): Int = this.toInt().and(0xFF)

internal fun Short.toUShort(): Int = this.toInt().and(0xFFFF)

internal fun Int.toUInt(): Long = this.toLong().and(0xFFFFFFFF)

internal fun Iterable<SubFolderMetaDto>.toSubFolderMetaNativeList(): SubFolderMetaNativeList {
    val destination = SubFolderMetaNativeList()
    for (item in this) {
        destination.add(item)
    }
    return destination
}

internal fun Iterable<SubMessageMetaDto>.toSubMessageMetaNativeList(): SubMessageMetaNativeList {
    val destination = SubMessageMetaNativeList()
    for (item in this) {
        destination.add(item)
    }
    return destination
}

internal fun Iterable<RecipientDto>.toRecipientNativeList(): RecipientNativeList {
    val destination = RecipientNativeList()
    for (item in this) {
        destination.add(item)
    }
    return destination
}

internal fun Iterable<AttachmentDto>.toAttachmentNativeList(): AttachmentNativeList {
    val destination = AttachmentNativeList()
    for (item in this) {
        destination.add(item)
    }
    return destination
}
After building my library doesn`t work, but if I remove at least one internal modifier my library works. I followed your advice and I had to do wrappers for kotlin types: List, ByteArray
a
I made a few more tries to reproduce your situation, but it keeps working okay. In my opinion, there is something wrong between your library file and the header. But I was assured that, because of their simultaneous produce, it couldn’t be the problem. Therefore, I want to ask you about two things: 1) Does your reproducer from YT work in the blank new project? (I mean both new C code and Kotlin one) 2) How do you include your library to the C program?