JACOB 没有正确释放对象

2022-01-14 00:00:00 com java jacob

我有一个 Eclipse 插件,它使用 Jacob 连接到一个 COM 组件.但是在我完全关闭插件后,.exe 文件仍然挂在 Windows 进程中.

I have an eclipse plugin, which connects to a COM component using Jacob. But after I close the plugin entirely, the .exe file stays hanging in Windows processes.

我使用 ComThread.InitMTA(true) 进行初始化,并确保在关闭应用程序之前为我创建的每个 COM 对象调用 SafeRelease() 并调用 <代码>ComThread.Release() 在最后.

I use ComThread.InitMTA(true) for initialization and make sure that SafeRelease() is called for every COM object I created before closing the app and I call ComThread.Release() at the very end.

我会留下一些未完成的事情吗?

Do I leave something undone?

推荐答案

TD2JIRA 转换器也有同样的问题.最终不得不修补其中一个 Jacob 文件以释放对象.之后一切顺利.

Had the same problem with TD2JIRA converter. Eventually had to patch one of the Jacob files to release the objects. After that all went smooth.

我的客户端 logout() 方法中的代码现在如下所示:

The code in my client logout() method now looks like this:

try {
  Class rot = ROT.class;
  Method clear = rot.getDeclaredMethod("clearObjects", new Class[]{});
  clear.setAccessible(true);
  clear.invoke(null, new Object[]{});
} catch( Exception ex ) {
  ex.printStackTrace();
}

最初无法访问 ROT 类,AFAIR.

The ROT class wasn't accessible initially, AFAIR.

更新

Jacob中释放资源的正确方法是调用

The correct way to release resources in Jacob is to call

ComThread.InitSTA(); // or ComThread.InitMTA()
...
ComThread.Release();

但不好的是,有时它无济于事.尽管 Jacob 调用了本机方法 release(),但内存(甚至不是 Java 内存,而是 JVM 进程内存)却无法控制地增长.

Bad thing though is that sometimes it doesn't help. Despite Jacob calls native method release(), the memory (not even Java memory, but JVM process memory) grows uncontrollably.

相关文章