简体   繁体   English

JNA-C内存错误使Java程序崩溃

[英]JNA - C memory error crashes Java Program

I am using JNA in order to call C functions from Java. 我正在使用JNA以便从Java调用C函数。 Sometimes the C functions are crashing and they are also making the java application to crash - an unwanted effect. 有时C函数崩溃了,它们还使Java应用程序崩溃了–这是不希望的结果。

If I run my program from the IDE I got the following message with exit code: 如果我从IDE运行程序,则会收到以下消息,并带有退出代码:

Process finished with exit code -1073740940 (0xC0000374) 进程完成,退出代码-1073740940(0xC0000374)

If i run the compiled java application I get a general error message: 如果我运行已编译的Java应用程序,则会收到一般错误消息:

Java错误

Do you know how could I prevent these type of errors from crashing the java program? 您知道如何防止这类错误使Java程序崩溃吗?

All calls are already wrapped in try- catch throwable objects . 所有调用已包装在try- catch可抛出对象中

I should mention that I have no control over the C program or the jna wrapper, I am just calling the methods. 我应该提到我无法控制C程序或jna包装器,我只是在调用方法。 (As a matter of fact I am using the wrapper tes4j for tesseract, and it crashes for certain images, but this did not seem relevant for finding a solution). (事实上​​,我正在使用包装tes4j进行tesseract,对于某些图像它崩溃了,但这似乎与寻找解决方案无关)。

Not possible. 不可能。

Your C code/libs are run natively inside your process, outside of the virtual machine. 您的C代码/库在虚拟机外部的进程内部本地运行。 In other words, errors occurring in that code will not be handled by the VM. 换句话说,该代码中发生的错误将不会由VM处理。

You may be able to get some ugly hack together by setting up your own signal handlers, but your process' in-memory state may still be messed up from wild pointers. 通过设置自己的信号处理程序,您可能会遇到一些丑陋的事情,但是您的进程的内存状态可能仍然被狂放的指针弄乱了。 Doing this is a really bad idea™ . 这样做是一个非常糟糕的主意 I mean, you know that the code is mishandling pointers, so what if it also writes to a valid (but wrong) location? 我的意思是,您知道代码对指针的处理不正确,那么如果它也写入有效(但错误)的位置怎么办? All kinds of terrible things could happen. 各种各样的可怕事情都可能发生。

It may be possible to write a small stand-alone C program and call it through Runtime.exec() . 可以编写一个小的独立C程序,然后通过Runtime.exec()对其进行调用。 That way, any crashes would just result in a failing return code from the child process. 这样,任何崩溃都只会导致子进程的返回码失败。 Of course, any complex return values or in-memory side effects will not be easy to achieve -- so whether this method is advisable or not depends on your use case. 当然,任何复杂的返回值或内存副作用都不容易实现-因此,此方法是否可取取决于您的用例。

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

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