samedi 3 juin 2017

Android: issues getting a c++ std::string up to JNIEXPORT gets unexpectedly cleared out

On an Android application, I'm trying to get a string from native code up to Java, but exactly at the JNIEXPORT level, the std::string gets wiped out of its contents. Here's the code for all the three layers:

C++ code (original x-patform class):

std::string GTAInterface::GetConfigurationJSON()
{
    m_cfgJSON = "a bare test";
    return m_cfgJSON;
}

C++ wrapper code (since JNI on Android can only call static C++ finctions... no words on this):

const char *gtaGateway::GetConfigurationJSON(int sysId)
{
    string ret = ((GTAInterface*)gtaSystemArray[sysId])->GetConfigurationJSON();
    return ret.c_str(); // here the "ret" string is still retaining its value
}

An finally, the JNIEXPORT class:

JNIEXPORT jstring JNICALL
Java_com_gta_sdk_gtaGateway_GetConfigurationJSON(JNIEnv *env, jobject obj, jint sys)
{
    std::string cfgJson = gtaGateway::GetConfigurationJSON(sys);

    return env->NewStringUTF(cfgJson.c_str()); // here the debugger shows that "cfgJson" is "" (empty)??!!
}

So, the code correctly gets executed throughout all the call chain, but for some reason I can't figure out why the "cfgJson" string @ the JNIEXPORT level gets cleared out! Could someone please help out, since I have no other clue of what I could be doing wrong...

Many thanks!

Aucun commentaire:

Enregistrer un commentaire