如何调用 JNI_CreateJavaVM 而不会出现 Valgrind 错误?
在 valgrind 下编译并运行以下代码时,我始终收到“Invalid write of size 4”错误。有没有一种干净的方法来调用 JNI_CreateJavaVM() 以便 valgrind 不会出现异常?
#include <jni.h>
#include <stdlib.h>
#include <string.h>
int main()
{
JavaVMInitArgs * vm_args = calloc(1, sizeof(JavaVMInitArgs));
JavaVM * jvm = NULL;
JNIEnv * env = NULL;
vm_args->version = JNI_VERSION_1_6;
vm_args->nOptions = 0;
vm_args->options = NULL;
JNI_CreateJavaVM(&jvm, (void **)&env, vm_args);
return 0;
}
这是我正在运行的 valgrind 命令:
valgrind --tool=memcheck --leak-check=yes --num-callers=20 --smc-check=all ./test
来自我的 loooonng valgrind 日志的单个条目:
==9004== Invalid write of size 4
==9004== at 0x4D5A3C8: ???
==9004== by 0x4D512CB: ???
==9004== by 0x423374F: JavaCalls::call_helper(JavaValue*, methodHandle*, JavaCallArguments*, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x4361B67: os::os_exception_wrapper(void (*)(JavaValue*, methodHandle*, JavaCallArguments*, Thread*), JavaValue*, methodHandle*, JavaCallArguments*, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x42335AE: JavaCalls::call(JavaValue*, methodHandle, JavaCallArguments*, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x420F8C5: instanceKlass::call_class_initializer_impl(instanceKlassHandle, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x420E650: instanceKlass::initialize_impl(instanceKlassHandle, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x420DB97: instanceKlass::initialize(Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x420E8AB: instanceKlass::initialize_impl(instanceKlassHandle, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x420DB97: instanceKlass::initialize(Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x440D660: Threads::create_vm(JavaVMInitArgs*, bool*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x4265395: JNI_CreateJavaVM (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x804845F: main (jvm.c:15)
==9004== Address 0xbeb54078 is not stack'd, malloc'd or (recently) free'd
谢谢, 陈兹
When compiling and running the following code under valgrind, I consistently get "Invalid write of size 4" errors. Is there a clean way of calling JNI_CreateJavaVM() so that valgrind doesn't throw a fit?
#include <jni.h>
#include <stdlib.h>
#include <string.h>
int main()
{
JavaVMInitArgs * vm_args = calloc(1, sizeof(JavaVMInitArgs));
JavaVM * jvm = NULL;
JNIEnv * env = NULL;
vm_args->version = JNI_VERSION_1_6;
vm_args->nOptions = 0;
vm_args->options = NULL;
JNI_CreateJavaVM(&jvm, (void **)&env, vm_args);
return 0;
}
Here is the valgrind command I am running:
valgrind --tool=memcheck --leak-check=yes --num-callers=20 --smc-check=all ./test
A single entry from my loooonng valgrind log:
==9004== Invalid write of size 4
==9004== at 0x4D5A3C8: ???
==9004== by 0x4D512CB: ???
==9004== by 0x423374F: JavaCalls::call_helper(JavaValue*, methodHandle*, JavaCallArguments*, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x4361B67: os::os_exception_wrapper(void (*)(JavaValue*, methodHandle*, JavaCallArguments*, Thread*), JavaValue*, methodHandle*, JavaCallArguments*, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x42335AE: JavaCalls::call(JavaValue*, methodHandle, JavaCallArguments*, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x420F8C5: instanceKlass::call_class_initializer_impl(instanceKlassHandle, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x420E650: instanceKlass::initialize_impl(instanceKlassHandle, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x420DB97: instanceKlass::initialize(Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x420E8AB: instanceKlass::initialize_impl(instanceKlassHandle, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x420DB97: instanceKlass::initialize(Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x440D660: Threads::create_vm(JavaVMInitArgs*, bool*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x4265395: JNI_CreateJavaVM (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004== by 0x804845F: main (jvm.c:15)
==9004== Address 0xbeb54078 is not stack'd, malloc'd or (recently) free'd
Thanks,
Chenz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于这是 JVM 内的错误,您可以选择抑制它。 Valgrind 可以配置为抑制特定错误。您可以在此处找到更多详细信息
Since this is an error within the JVM you can just choose to supress it. Valgrind can be configured to supress specific errors. You can find more details here