Search code examples
cjava-native-interfacefree

jni-wrapper crashes JVM on Windows


I have written a JNI-Wrapper but used it till today only on Linux. Now I compiled it for windows and the JVM crashes in this native methods: If I remove the ReleaseStringUTFChars and free calls it also works on Windows, but It would be interesting why this works in linux but not in windows?(I use Windows 10-64bit). And from my experience this calls are mandatory, got some memory-leaks(on linux) at the beginning before I released them correctly

void onMessageReceived(char* topic, char* timestamp, char* id, char* value) {
JNIEnv * g_env;
int getEnvStat = (*g_vm)->GetEnv(g_vm, (void **) &g_env, JNI_VERSION_1_8);
if (getEnvStat == JNI_EDETACHED) {
    if ((*g_vm)->AttachCurrentThread(g_vm, (void **) &g_env, NULL) != 0) {
        puts("Failed to attach");
        fflush(stdout);
    }
}

if (methodHandleMessage) {
} else {
    jclass clazz = (*g_env)->GetObjectClass(g_env, store_callback);
    methodHandleMessage = (*g_env)->GetMethodID(g_env, clazz, "handleMessage", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
}
jstring j_topic = (*g_env)->NewStringUTF(g_env, topic);
jstring j_timestamp = (*g_env)->NewStringUTF(g_env, timestamp);
jstring j_value = (*g_env)->NewStringUTF(g_env, value);
jstring j_id = (*g_env)->NewStringUTF(g_env, id);


(*g_env)->CallVoidMethod(g_env, store_callback, methodHandleMessage, j_topic, j_timestamp, j_id, j_value);

//HERE IS THE PROBLEM:
(*g_env)->ReleaseStringUTFChars(g_env, j_topic, topic);
(*g_env)->ReleaseStringUTFChars(g_env, j_timestamp, timestamp);
(*g_env)->ReleaseStringUTFChars(g_env, j_value, value);
(*g_env)->ReleaseStringUTFChars(g_env, j_id, id);
//

(*g_vm)->DetachCurrentThread(g_vm);
}

and

void rep_actor(zsock_t *pipe, void *args) {
zsock_signal(pipe, 0);
while (!zsys_interrupted) {
    char* timestamp;
    char* sender;
    char* command;
    char* message;

    zsock_recv(reply, "ssss", &timestamp, &sender, &command, &message);

    char* result = onRequestReceived(timestamp, sender, command, message);
    zsock_send(reply, "s", result);

//HERE IS THE PROBLEM:

    free(timestamp);
    free(sender);
    free(command);
    free(message);
    free(result);
//

}
}

Solution

  • The error happens because you free memory twice. First time - by calling ReleaseStringUTFChars() inside of onMessageReceived(), second - outside of onMessageReceived() you call free() on every released pointer.

    Important
    You don't need release memory manually for objects created inside of java enviroment (calling of env->New*() methods). In your case:

    // j_topic - it's a pointer for object inside of java heap
    // garbage collector will free this memory
    jstring j_topic = env->NewStringUTF(topic);
    

    Call of ReleaseStringUTFChars() is inapropriate here. This used only when you create native string from java String using GetStringUTFChars():

    // j_topic - string inside of java heap
    const char* native_str = env->GetStringUTFChars(j_topic, isCopy);
    
    // your logic for native_str array
    
    // informs the VM that the native code no longer needs access to native_str
    env->ReleaseStringUTFChars(j_topic, native_str);
    

    Look at javadoc for methods GetStringUTFChars() and ReleaseStringUTFChars() - you should use they only together.