E
E
Evgeny Ivanovich2015-07-11 13:46:15
Java
Evgeny Ivanovich, 2015-07-11 13:46:15

Permanently saving a pointer to char* from QString, how?

In general, there was nothing to do in the evening, and one respected one wanted to connect JNI to the project.
But it wasn’t there, it turns out that the JVM takes a special structure with options as input, in which the string is a pointer to the string (char * ).. Without thinking for a long time, I convert the whole thing to char* like this.

JavaVMOption options[alength];

    foreach(QString param, jargs)
    {
       static int i = 0;
       QByteArray arr = param.toLocal8Bit();
       char * pbuff = arr.data();
       options[i].optionString = pbuff;
       i++;
    }

Where jargs is QStringList. And so, the pointer that is issued, but also at some point is lost
. Selected values ​​on lines in structure are shown. JavaVM accordingly also swears "Unrecognized option: ...".
306b2897f8e7421f9505f6985eaf0fcd.png
Here are the structures
typedef struct JavaVMOption {
    char *optionString;
    void *extraInfo;
} JavaVMOption;

typedef struct JavaVMInitArgs {
    jint version;

    jint nOptions;
    JavaVMOption *options;
    jboolean ignoreUnrecognized;
} JavaVMInitArgs;

And here is the full code of the method.
/**
 * Метод запуска виртуальной среды java.
 * @brief Jvm::launchJVM
 */
bool Jvm::launchJVM()
{
    jint alength = (jint)jargs.length();

    __debug_p("Args lenght" << alength);

    if(alength < 1)
    {
        __DEBUG_P("No java options");
        return false;
    }

    __DEBUG_P("Launching Java VM...");

    JavaVMOption options[alength];

    foreach(QString param, jargs)
    {
       static int i = 0;
       //options[i].optionString = param.toLatin1().data();
       QByteArray arr = param.toLocal8Bit();
       char * pbuff = arr.data();
       options[i].optionString = pbuff;
       //options[i].optionString = const_cast<char*>(param.toStdString().c_str());
       i++;
    }

    for(int i = 0; i < alength; i++)
    {
        qDebug() << "%$$%$%$%POINTER IN STRUCT" << options[i].optionString << endl;
    }

    JavaVMInitArgs vm_args;

    jint ret = JNI_GetDefaultJavaVMInitArgs(&vm_args);

    if(ret == JNI_ERR)
        __debug_p("Error while loading default jvm init args");

    vm_args.version = JNI_VERSION_1_6;
    vm_args.options = options;
    vm_args.nOptions = alength;
    vm_args.ignoreUnrecognized = JNI_FALSE;

    jint code = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
    if(code == JNI_ERR)
    {
        __DEBUG_P("JVM is not runned, error code" << code);
        return false;
    }

    __DEBUG_P("JVM runned successful");

    emit JavaVMRunned(env);
    return true;
}

How to be?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MiiNiPaa, 2015-07-11
@Pauk_Code

Of course it gets lost. You have arr local to the loop body. As soon as the next iteration ends, the resulting pointer becomes invalid.
Make sure the memory referenced by the pointer is not freed until the JVM is done with it.

J
Jacob E, 2015-07-11
@Zifix

Dear, most likely you need something like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question