1

In thinking about calling java objects how does one prevent java from changing the location of the object? In C++ one asks the JVM for a reference to an object an then you typecast it to a pointer. So the problem is obvious, your pointer is no longer valid. Any idea's?

In looking at the answers thus far let me clarify. As I understand it, when java creates an object it provides a reference ( an equivalent to a pointer ) and that reference leads all other objects to its actual data and methods. When calling java from within C++ you have to type cast the reference to a pointer and thus you have the location, in memory, of the object.

Java maintains its memory pools as cleanly as possible to optimize execution / heap fragmentation, etc. In doing so it may very well move objects to different memory pools, thus changing the actual memory address the reference points to ( internally, in the jvm ) and updates the reference list, so that all the other java objects can still access the same objects correctly.

My question comes to the point when in C++, since their is no contract between the C++ runtime and the JVM, you cast the reference to a pointer that now points to a physical address in memory. If the JVM relocates the object in the process of optimization, would that pointer in C++ now be invalid in a long running java program?

I hope this helps clarify the question.

FlyingGuy
  • 333
  • 1
  • 9
  • "Changing the location"? What does that mean? Java doesn't have pointers like C/C++. References refer to objects living on your heap, nowhere else. – duffymo Jul 21 '14 at 22:30
  • Is your question about how Java can relocate objects while C++ cannot, or how, if you're using JNI to interface with the JVM, the pointers you use don't end up going bad? – templatetypedef Jul 21 '14 at 22:30
  • See also http://stackoverflow.com/questions/8764683/is-it-possible-to-find-the-address-of-a-non-primitive-variable-in-java – Raedwald Jul 21 '14 at 22:32
  • 2
    For all practical purposes you can assume that a reference stays valid for the lifetime of the program, even when garbage collection happens. How exactly the memory area containing the object referred to by the reference is entirely up to the JVM, and you shouldn't worry. (Until you start working with JNI, but that is an advanced subject). – Thorbjørn Ravn Andersen Jul 21 '14 at 22:36
  • See also http://stackoverflow.com/a/1961150/545127 – Raedwald Jul 21 '14 at 22:39
  • See also http://stackoverflow.com/questions/20249723/is-the-address-of-an-object-fixed-during-its-life-cycle – Raedwald Jul 21 '14 at 22:45
  • @RaedWald - Yes that is exactly my question. The jvm moves things around to suit its needs. C++ just needs to be able to make method calls reliably. The task will be calling a large collection of classes that interact ( approximately 20 different packages and 100's of classes ). – FlyingGuy Jul 23 '14 at 01:32

1 Answers1

1

You seem to be asking about JNI methods. Assuming that's the case, the JVM 'pins' a JNI argument object or an object created by a JNI method to the same address for the duration of the method. If you want to save a JNI object reference between JNI calls and have it be valid during the next call you have to use a GlobalRef.

EDIT I've answered your question as edited, but this:

In C++ one asks the JVM for a reference to an object an then you typecast it to a pointer.

is not correct, and neither is this:

When calling java from within C++ you have to type cast the reference to a pointer and thus you have the location, in memory, of the object

or this:

you cast the reference to a pointer that now points to a physical address in memory.

You never have the address of the object in JNI. You have an opaque value supplied by the JVM as a jobject. What it contains is entirely unspecified, and no typecast mechanism to derive a memory address is specified anywhere in the JNI Specification.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Perhaps my ignorance in the matter prevents me from understanding your reply. It is/was my understanding the JNI is used by Java for calling external libraries / functions written in languages other than java. I on the other hand am trying to determine the viability / advisability of calling java friom within C++. – FlyingGuy Jul 22 '14 at 01:46
  • Wrong. Calling Java from C++ is still JNI. – user207421 Jul 22 '14 at 02:12
  • @EJP "the JVM 'pins' a JNI argument object or an object created by a JNI method to the same address for the duration of the method." -- This is absolutely wrong when talking about HotSpot JVM. As I told you earlier, HotSpot does not support object pinning. Instead it updates `jobject` handles whenever a corresponding object is moved by GC. – apangin Jul 22 '14 at 05:37
  • @apangin I don't know what prior conversation you're referring to, but the statement concerned is more or less straight out of the [JNI Specification](https://community.oracle.com/community/developer/english/java): "A registry maps nonmovable local references to Java objects". The point being that you can't reuse `jobject` values across JNI invocations without a `GlobalRef`. – user207421 Jul 23 '14 at 01:52
  • @apangin Both of you are right. Have a look at this. http://mail.openjdk.java.net/pipermail/hotspot-dev/2016-September/024464.html – Jenix Apr 02 '18 at 17:37