• Post Reply Bookmark Topic Watch Topic
  • New Topic

Do I need to delete JNIEnv/JavaVM pointers? DeleteGlobalRef on JavaVM?  RSS feed

 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JNI do I need to either delete the pointers or call DeleteGlobalRef for them?

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, you don't do either. It's just an ordinary pointer.

DeleteGlobalRef is for JNI C++ code to release a reference to a Java object. The JVM certainly isn't a Java object, so DeleteGlobalRef would be wrong.

The C++ delete operator is for deleting C++ objects that (a) were allocated with C++ new operator (not malloc, LocalAlloc...), and (b) you own. In the case of the memory pointed to by the JavaVM*, you don't know how it was allocated and you do not own it.

The JavaVM* pointer will be usable in any thread, as long as the JVM is alive. You have to ensure that you don't use it after the JVM shuts down.

You could perhaps implement JNI_OnUnload and set your stored JavaVM* to NULL when it gets called. If you have any functions that might try to use the JavaVM* after unload has happened, you'd need to test the JavaVM* for non-NULL before using it.
[ April 17, 2008: Message edited by: Peter Chase ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!