zeromq / jzmq

Java binding for ZeroMQ
http://www.zeromq.org
GNU General Public License v3.0
590 stars 364 forks source link

bug on sendzerocopy #431

Closed peterveron closed 8 years ago

peterveron commented 8 years ago

In socket.cpp. JNIEnv cannt be cached and used in another thread, it may cause jvm crush. I fix it like this: typedef struct _jzmq_zerocopy_t { JavaVM *jvm; jobject ref_buffer; } jzmq_zerocopy_t;

static void s_delete_ref (void _ptr, void hint) { jzmq_zerocopy_t free_hint = (jzmq_zerocopy_t )hint; JNIEnv env; freehint->jvm->AttachCurrentThreadAsDaemon((void *)&env, NULL); env->DeleteGlobalRef(free_hint->ref_buffer); delete free_hint; }

if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,0,0)

static jboolean s_zerocopy_init (JNIEnv env, zmq_msg_t message, jobject obj, jint length) { jobject ref_buffer = env->NewGlobalRef(obj); jzmq_zerocopy_t *free_hint = new jzmq_zerocopy_t;

JavaVM *gs_jvm;  
env->GetJavaVM(&gs_jvm);

free_hint->jvm = gs_jvm;
free_hint->ref_buffer = ref_buffer;

jbyte* buf = (jbyte*) env->GetDirectBufferAddress(ref_buffer);

int rc = zmq_msg_init_data (message, buf, length, s_delete_ref, free_hint);
if (rc != 0) {
    int err = zmq_errno();
    raise_exception (env, err);
    return JNI_FALSE;
}
return JNI_TRUE;

}

peterveron commented 8 years ago

repeat commit