> Hi,
> 
> JNI_OnLoad has a companion JNI_OnUnload [1], which is the perfect place
> to invoke an env->DeleteGlobalRef(MyWhateverClass), allowing the JVM
> garbage collector to do a proper cleanup.

That method is called "when the class loader containing the native library is 
garbage collected." However the class loader cannot be GCed, because it is 
referenced from one of the `java.lang.Class` objects it loaded. That class is 
then referenced by `env->NewGlobalRef(clazz);` - e.g. it a global GC root. As 
the `ClassLoader` is never GCed, the `JNI_OnUnload` is never called and cannot 
do the cleanup.

Memory leak, I am afraid.
-jt


> [1]
> https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/invocation.h
> tml#JNI_OnUnload
> El 8/4/21 a las 5:28, Jaroslav Tulach escribió:
> >> JNI_OnLoad(...) {
> >> 
> >>     // Find the class in OnLoad
> >>     jclass clazz = env->FindClass(...);
> >>     // And keep a NewGlobalRef for future use...
> >>     MyWhateverClass = env->NewGlobalRef(clazz);
> >> 
> >> ...
> > 
> > That's the recommended approach. However it shall be stated that this is
> > creating a (class loading) memory leak - it is not going to be possible to
> > unload the JavaFX JAR file classes once this variable is initialized. Just
> > FYI, I think we can live with such behavior.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 
> For further information about the NetBeans mailing lists, visit:
> https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists





---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists



Reply via email to