• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

felonius

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

4 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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();



0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites