Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNI_CreateJavaVM: Buffer overrun if I throw an exception in case of failure

In a C++ project, I use the JNI invocation API to launch a JVM. I've done a little wrapper arount the JVM so I can use all the needed parts in a OO fashion. So far that works great.

Now, if the JVM does not start (JNI_CreateJavaVM returns a value < 0) I'd like to raise an exception within my C++ code.But if I throw an exception after JNI_CreateJavaVM, I get a buffer overrun. If I raise the exception without the JNI_CreateJavaVM call, it works as expected.

Does anyone have a clue on what the issue could be here? Or how to debug this?

Environment: Windows, Visual Studio 2008 JDK: jrockit27.6jdk16005, but happens with SUN stock one as well

Cheers Dominik

like image 567
Dominik Fretz Avatar asked May 31 '10 09:05

Dominik Fretz


1 Answers

Looks to me like you are throwing a pointer or reference to an invalid memory. It's a good idea to throw an exception by reference, but make sure that the object is not on the stack. If the object was allocated using 'new', you'll need to manage this properly (otherwise you'll have leaks). My approach is to try and throw const objects as much as possible.

Does this help?

like image 175
Android Eve Avatar answered Oct 28 '22 13:10

Android Eve