Note that there are some explanatory texts on larger screens.

plurals
  1. POJVM does not work as expected with JNI C++ code containing a class named "Node"
    primarykey
    data
    text
    <p>Myself and some teammates have been unable to understand why the following snippet of code will not give the correct output when using JVMs versions 1.6u23 through 1.6u31 (the latest as of this posting). This code snippet represents a simplification of a larger problem:</p> <p>UPDATE: Modified the example slightly to put focus on the issue that "virtual_function()" does not seem to get called.</p> <p>UPDATE: Simplified the example even more based on comments to-date.</p> <p>NodeTester.cpp:</p> <pre><code>#include &lt;iostream&gt; #include &lt;jni.h&gt; class Node { public: Node () :m_counter(0) {} virtual ~Node () {} virtual void virtual_function () { m_counter += 10; } void non_virtual_function () { m_counter += 1; } int get_counter () { return m_counter; } private: int m_counter; }; extern "C" { JNIEXPORT void JNICALL Java_NodeTester_testNode (JNIEnv *jni_env_rptr, jclass java_class) { Node *node_rptr = new Node(); node_rptr-&gt;non_virtual_function(); node_rptr-&gt;virtual_function(); std::cout &lt;&lt; node_rptr-&gt;get_counter() &lt;&lt; std::endl; delete node_rptr; } } </code></pre> <p>NodeTester.java:</p> <pre><code>public class NodeTester { public static native void testNode (); static { System.loadLibrary("nodetester"); } public static final void main (String[] args) { NodeTester.testNode(); } } </code></pre> <p>expected output:</p> <pre><code>11 </code></pre> <p>actual output with JVM 1.6u23 through 1.6u31:</p> <pre><code>1 </code></pre> <p>It seems like the JVM is incorrectly constructing the "Node" object within JNI; although it's possible that this code has something incorrect about its use of JNI. When the class "Node" gets more functionality added to it (e.g. more attributes, additional virtual and non-virtual operations), we can cause a segmentation fault, rather than just incorrect output. We're compiling the cpp code into a RedHat linux 64-bit shared object library using g++, and running the java code with the 64-bit Server VM. Note that on JVMs 1.6u20 through 1.6u22, this produces expected output. I haven't tried any earlier versions.</p> <p>We've decided to put a bounty on this question! Here's more information on what we already know:</p> <ul> <li>JVMs 1.6u22 (and prior) produce expected results</li> <li>Renaming "Node" or putting it in a namespace produces expected results</li> <li>Allocating a "Node" object on the stack instead of the heap in the JNI function produces expected results</li> <li>There are no issues with non-virtual components of the class "Node"</li> </ul> <p>Unfortunately for us, none of these items lead to viable solutions - the "larger problem" I alluded to was that we're dealing with a large, existing code base with a C++ class named "Node", which we need to access via JNI. We also tried several g++ and javac compiler options, and several JVM options, to no avail (although if someone stumbles on one that actually yields expected results, this would be an acceptable solution).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload