Android JNI原生C ++代码GetPrimitiveArrayCritical()问题

时间:2011-09-19 07:27:50

标签: android java-native-interface bytearray

我想编写一个本机方法,可以从socket接收数据,然后写回ByteArray,它是来自Java的输入参数。因为我在Java端分配ByteArray,并将其传递给本机方法,我知道我应该使用GetPrimitiveArrayCritical()来获取此ByteArray的指针。

但是,我仍然需要在线程中运行此过程,因此我使用名为socket_loop_native_data的结构的jbyteArray变量来记住此输入ByteArray,并使用pthread_create()以此结构作为输入参数启动新线程。

最后,在线程的主体代码中,我使用GetPrimitiveArrayCritical()来获取前一个ByteArray的指针,但它失败了......

错误日志是:

14:09:32.350 Warning dalvikvm 2780  JNI WARNING: 0x4077ab48 is not a valid JNI reference
14:09:32.350 Warning dalvikvm 2780               in Ldalvik/system/NativeStart;.run ()V (GetPrimitiveArrayCritical)

我的本​​机代码就像是

struct socket_loop_native_data {
        pthread_mutex_t thread_mutex;
        pthread_t thread;
        struct pollfd *pollData;
        JavaVM *vm;
        int envVer;
        jobject me;
        jbyteArray javaBuffer;
        int bufferSize;
        jbyte *nativeBuffer;
        char *beginOfBuffer;
        char *endOfBuffer;
        int decodedDataSize;
        bool running;
};

typedef socket_loop_native_data native_data_t;

static jfieldID field_mNativeDataSocket;

static inline native_data_t *get_native_data(JNIEnv *env, jobject object) {
    return (native_data_t *)(env->GetIntField(object, field_mNativeDataSocket));
}

native_data_t *get_SocketLoop_native_data(JNIEnv *env, jobject object) {
    return get_native_data(env, object);
}

JNIEXPORT void JNICALL Java_android_classInitNativeSocket(JNIEnv* env, jclass clazz) {
    field_mNativeDataSocket = env->GetFieldID(clazz, "mNativeDataSocket", "I");
}

JNIEXPORT void JNICALL Java_android_initializeNativeDataNativeSocket(JNIEnv* env, jobject object) {

    native_data_t *nat = (native_data_t *)calloc(1, sizeof(native_data_t));
    if (NULL == nat) {
        LOGD("%s: out of memory!", __FUNCTION__);
        return;
    }
    memset(nat, 0, sizeof(native_data_t));

    pthread_mutex_init(&(nat->thread_mutex), NULL);

    env->SetIntField(object, field_mNativeDataSocket, (jint)nat);

}

JNIEXPORT jboolean JNICALL Java_android_startSocketLoopNative(JNIEnv *env, jobject object, jint sock, jbyteArray buffer, jint size) {

    jboolean result = JNI_FALSE;

    socket_loop_native_data *nat = get_native_data(env, object);

    pthread_mutex_lock(&(nat->thread_mutex));

    nat->running = false;

    if (nat->pollData) {
        LOGD("trying to start SocketLoop a second time!");
        pthread_mutex_unlock( &(nat->thread_mutex) );
        return JNI_FALSE;
    }

    nat->pollData = (struct pollfd *)malloc(sizeof(struct pollfd));
    if (!nat->pollData) {
        LOGD("out of memory error starting SocketLoop!");
        goto done;
    }

    memset(nat->pollData, 0, sizeof(struct pollfd));

    nat->pollData[0].fd = sock;
    nat->pollData[0].events = POLLIN;

    env->GetJavaVM( &(nat->vm) );
    nat->envVer = env->GetVersion();

    nat->me = env->NewGlobalRef(object);

    nat->javaBuffer = buffer;
    nat->bufferSize = (int)size;
    nat->decodedDataSize = 0;

    pthread_create(&(nat->thread), NULL, socketLoopMain, nat);
    result = JNI_TRUE;

done:
    if (JNI_FALSE == result) {
        if (nat->me) env->DeleteGlobalRef(nat->me);
        nat->me = NULL;
        if (nat->pollData) free(nat->pollData);
        nat->pollData = NULL;
    }

    pthread_mutex_unlock(&(nat->thread_mutex));

    return result;
}

static void *socketLoopMain(void *ptr) {

    native_data_t *nat = (native_data_t *)ptr;
    JNIEnv *env;

    JavaVMAttachArgs args;
    char name[] = "SocketLoop";
    args.version = nat->envVer;
    args.name = name;
    args.group = NULL;

    nat->vm->AttachCurrentThread(&env, &args);

    ...

    nat->nativeBuffer = (jbyte *)(env->GetPrimitiveArrayCritical((nat->javaBuffer), NULL));     

    nat->beginOfBuffer = (char *)(&(nat->nativeBuffer[0]));
    nat->endOfBuffer = (char *)(&(nat->nativeBuffer[0])) + (nat->bufferSize);

    ...
}

执行语句

后VM中止
nat->nativeBuffer = (jbyte *)(env->GetPrimitiveArrayCritical((nat->javaBuffer), NULL));

所以... Dose意味着我可以/不应该使用jbyteArray变量来记住输入ByteArray并在另一个地方调用GetPrimitiveArrayCritical()???

或者,我忘了做什么?

任何建议都将非常感谢!!!

1 个答案:

答案 0 :(得分:2)

您似乎忘记了NewGlobalRef指向nat->javaBuffer中字节数组startSocketLoopNative的指针。

这应该是你的麻烦的原因,因为你不在同一个线程中(新的env是由AttachCurrentThread创建的,根据我的经验,所有以前的非全局变量都是无效的)


旁注:永远不会在JNI / Android下开发,所以对于一个总是编码J2SE / JNI的人来说,你的代码似乎很奇怪:)