简体   繁体   English

Java JNI Stackfall预防

[英]Java JNI stackfall prevention

I use Java JNI with Gdal . 我将Java JNI与Gdal一起使用 There are some server side applications built on top of the JNI bindings. 在JNI绑定之上构建了一些服务器端应用程序。 The whole JVM stackfalls if there is an error in the JNI section. 如果JNI节中有错误,则整个JVM堆栈将下降。

What's the best way of testing that ac/c++ library does not contain fatal errors which will cause JVM stackfall ? 测试ac / c ++库不包含会导致JVM堆栈下降的致命错误的最佳方法是什么? What is the best practice to cleanly deal with errors which do arise ? 彻底解决出现的错误的最佳实践是什么?

Have you got these JVM settings turned on? 您是否已打开这些JVM设置?

-verbose:jni -Xcheck:jni

I find them invaluable for initial development of JNI code. 我发现它们对于JNI代码的最初开发非常宝贵。

The -Xcheck:jni option activates a set of wrapper functions around the JNI functions. -Xcheck:jni选项激活围绕JNI函数的一组包装器函数。 The wrapper functions perform checks on the incoming parameters. 包装函数对传入的参数执行检查。 These checks include: 这些检查包括:

  • Whether the call and the call that initialized JNI are on the same thread. 该调用和初始化JNI的调用是否在同一线程上。
  • Whether the object parameters are valid objects. 对象参数是否为有效对象。
  • Whether local or global references refer to valid objects. 本地引用还是全局引用都引用有效对象。
  • Whether the type of a field matches the Get<Type>Field or Set<Type>Field call. 字段的类型是否匹配Get<Type>FieldSet<Type>Field调用。
  • Whether static and nonstatic field IDs are valid. 静态和非静态字段ID是否有效。
  • Whether strings are valid and non-null. 字符串是否有效和非空。
  • Whether array elements are non-null. 数组元素是否为非null。
  • The types on array elements. 数组元素上的类型。

Two things come to mind: 我想到两件事:

1). 1)。 If you have any possibility to run the C++ code as a separate process (use any RPC technique to communicate) then you dodge this problem, albeit at the expense of performance. 如果您有可能将C ++代码作为一个单独的进程运行(使用任何RPC技术进行通信),则可以避开此问题,尽管会牺牲性能。

2). 2)。 You are at the mercies of the C++/JNI developer. 您是C ++ / JNI开发人员的摆布。 Most problems I've encourntered are at the "skin" of the JNI layer. 我提倡的大多数问题都在JNI层的“皮肤”上。 That is, I had a reasonbably stable, existing library which I wrapped in JNI. 也就是说,我有一个相当稳定的现有库,我将其包装在JNI中。 The library would get upset if I inadvertantly passed null pointers to the existing code, and if I failed to check for nulls in response I could also have issues. 如果我无意中将空指针传递给了现有代码,该库将变得不高兴,并且如果我未能检查空值作为响应,我也会遇到问题。 So we put a lot of effort into sanitising that wrapper layer. 因此,我们付出了很多努力来清理该包装层。 Anywhere where there was a possibility for unexpected results we added checking. 在可能出现意外结果的任何地方,我们都添加了检查功能。

Of course if the whole library is new, then life is harder - in the end you simply have to produce robust code. 当然,如果整个库是新库,那么生活会更艰难-最后,您只需要生成可靠的代码即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM