Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


[java] How do I use JNI Weak Global References?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 felonius   Members   -  Reputation: 122

Like
Likes
Like

Posted 20 November 2000 - 04:07 AM

Hi, A JNI Weak global reference is a reference from C++ to a Java object, but this reference does not prevent the Java object from being garbage collected. The catch: The object may be garbage collected at any time turning the reference illegal. I am a bit at loss on how to use the Weak Global References, though. Assume that the jobj variable contains a Weak Global Reference that I want to use. According to the docs I can use IsSameObject() to check if it is legal, and then use it, but what if the object is garbage collected before I actually get around using it? if (!env->IsSameObject(jobj,NULL)) { ...Use jobj in JNI call. } In the above code I might potentially risk that the object is garbage collected between the test and the use of jobj. How can I ensure that a reference placed in a weak global reference variable is legal when I need it? I have come up with the following, but I don''t know if it work at all times. Will it work? Or is the call to NewLocalRef illegal if the weak Global Reference has points to NULL. jobject o = env->NewLocalRef(jobj); if (o!=NULL) { ...Use o in JNI call. } Any help or advice would be appreciated. Jacob Marner

Sponsor:

#2 snowmoon   Members   -  Reputation: 122

Like
Likes
Like

Posted 20 November 2000 - 04:44 AM


Or would it be...

jobject o = env->NewLocalRef(jobj);
if ( !env->IsSameObject(o,NULL))
{
....
}

Just a thought. The new local ref should lock the object from being garbage collected, then you confirm that it is not null in the way the docs suggect and then use it.

#3 felonius   Members   -  Reputation: 122

Like
Likes
Like

Posted 20 November 2000 - 06:46 AM

Thanks. This might very well be the solution.

The only $10.000 question remaining is whether a local reference can point to a reference that is the same as NULL, as this seems to be a property special to the weak global references? I am asking, because in the docs it says that NewLocalRef() can take a global reference and local reference. They don''t mention weak global references.

I have tested, and it works, that NewLocalRef() can be used with weak global references that are not NULL, but what if they are NULL? I have been unable to construct a proper test example (at least without using a lot of time)

And by the way, the reason why I wrote

...
if (o!=NULL)
...

is because the docs for NewLocalRef() says that a NULL argument gives a NULL result, but it is not a weak reference is not NULL, but a NULL reference, so this is wrong.

So if what you say is right snowmoon, we must assume that Java has some special NULL reference and NULL references can be manipulated but not used. I am right?

Jacob Marner


#4 snowmoon   Members   -  Reputation: 122

Like
Likes
Like

Posted 20 November 2000 - 11:36 AM

I''de have to assume that IsSameObject is a wrapper so that the jvm can run on many platforms o!=null might work on 99% of the platforms, but why risk the fach that they could change this to point to an on-disk reprenstation of the object in the future and IsSameObject or other api calls bring them back.

The easiest way yo test is using a forced out-of-memory exception.

Psuedocode.

CreateGlobalRef();

Vector v = new Vector();
try {
for(; {
v.add ( new String("This is a test") );
}
} catch ( OutOfMemoryException e ) {
// GlobalWeakRef should have been dealocated BEFORE out of memory condition occured
v = null;
}

CheckGlobalRef();





#5 felonius   Members   -  Reputation: 122

Like
Likes
Like

Posted 20 November 2000 - 09:02 PM

I have just read an article on Java reference objects at JDC at:
http://developer.java.sun.com/developer/technicalArticles/ALT/RefObj/index.html

That assumption can''t be right, snowmoon. o!=NULL can''t be the same as IsSameObject(o,NULL), ever.
A NULL reference can''t just be a jobject variable that is NULL.

Say I write this:

jobject j = env->NewWeakGlobalRef(mydata);
jobject k = j;
... (is garbage collected)

The garbage collector knows nothing of the location of either j and especially not k, so these cannot be NULL. They must be non-NULL but be pointers to something that is registered as being deleted.

The docs actually says, I found, that weak references can be used in JNI functions like any other reference, so we must assume that they can be used in NewLocalRef too.

So I coming to the conclusion that the code you posted first is the only proper solution to use and that the IsSameObject() test is required on all platform for this to work.

Thanks again, snowmoon. That was really a help.

Jacob Marner




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS