Avoid NullPointerException when starting JNI VM

EDIT: As Amangin pointed out in the comments, the assumption that it was NPE is wrong.

I am working on a large project which forced to use JVM which is called from C ++ code. This is the JVM creation method:

jvm_holder jvm_holder::create_jvm(std::vector<std::string> options) {

            /*
             * args args that produce the behaviour:
             *      {
             *          std::string("-Djava.class.path=./Assistant-0.1.0.jar"),
             *          std::string("-Xrs"),
             *          std::string("-XX:+UseAltSigs") <- this is not useful on linux, we know but we tried it anyways 
             *      };
             */

            if (nullptr != jvm){
                throw internal_exception("If you get this message during development, please read up on the usage of the JVM Holder!");
            }

            jvm_holder main_holder;

            JavaVMInitArgs vm_args;

            JavaVMOption *opt = new JavaVMOption[options.size()];
            for (int i = 0; i < options.size(); i++){
                opt[i].optionString = (char *) options[i].c_str();
            }
            vm_args.version = JNI_VERSION_1_8; 
            vm_args.nOptions = (int) options.size();
            vm_args.options = opt;
            vm_args.ignoreUnrecognized = JNI_FALSE;
            jint rc = JNI_CreateJavaVM(&jvm, (void **) &main_holder.env, &vm_args);
            delete opt;
            if (rc != JNI_OK) {
                jvm_exception("Can not start JVM.");
            }
            return main_holder;
        }

      

Now, every time this code is executed (which is exactly one time when the program is run), there is an internal NullPointerException during JVM installation. This NPE is handled internally by the JVM and it is not very important to the process itself.

BUT: Every process in a process group receives a SIGSEGV. I understand that this is the normal mechanism for the JVM to detect NPEs.

Now there are some problems with this method:

  • Another JVM that is in the same process group also handles NPEs.
  • Whenever one of us uses GDB, the process is suspended, which is very annoying. We could force GDB to ignore the signal, but we think this is a bad idea as it means a problem elsewhere in the project.
  • We fear that this will lead to problems later in the project.

Now we haven't figured out why this particular NPE, but is there any option to disable signal-based NPE-based processing or avoid what leads to this startup issue? I can't imagine this didn't bother anyone, as this is the third time we've run into the problem in just a few months, and this time we can't just stop using JNI.

Regards

+3


source to share





All Articles