JNI: properly manage lifetime of a Java object

前端 未结 2 614
轻奢々
轻奢々 2021-01-15 01:31

When I create a native implementation peer in c++ how can I ensure that native part is also deleted when java object is deleted by JVM? I can add some methods that user of j

相关标签:
2条回答
  • 2021-01-15 02:24

    Please be aware that by embracing finalizers, you are tickling JVM in the worst possible place. Invoking JNI in finalizer() is like adjusting engine valve timing while driving. While it is technically possible, it is also very easy to end up with leaking JVM or a crash. With even slightest touch of multithreading, you will deadlock/crash at some point for sure. If you say "i don't want anything fancy", i would suggest going with explicitly called methods. Yes requiring certain method order by convention is dirty design, but then JNI is dirty in general.

    If a respected JNI library does an undercover class instrumentation instead of simply using finalizers, it perhaps has a reason. Some recommended reading (and that does not even mention JNI!):

    http://asserttrue.blogspot.com/2008/11/finalization-is-evil.html

    http://elliottback.com/wp/java-memory-leaks-w-finalize-examples/

    My suggestion is: yes implement a baseclass with a virtual method detachFromPeer(), set some "detached" flag in it and then in finalizer just check the flag with some warning.

    0 讨论(0)
  • 2021-01-15 02:37

    This is a rare case where you should really use a finalizer, but you should also make your Java class Closeable as well.

    0 讨论(0)
提交回复
热议问题