简体   繁体   English

JNA可用内存在共享对象dll中创建

[英]JNA free memory created in the shared object dll

I tried libvirt java bindings and always get Access Violation error when the library try to use Native.free method to free memory allocated in the dll, so I write the following simple test. 我尝试了libvirt java绑定并且当库试图使用Native.free方法释放在dll中分配的内存时总是会出现Access Violation错误,所以我编写了以下简单的测试。

public class HelloWorld {

    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary) Native.loadLibrary("msvcrt", CLibrary.class);

        Pointer _strdup(String src);
    }

    public static void main(String[] args) {
        Pointer p = CLibrary.INSTANCE._strdup("HelloWorld");
        Native.free(Pointer.nativeValue(p));
    }
}

My environment is as following: Windows 7 Ultimate SP1 64Bit, Jdk&Jvm is also 64bit(1.8.0_101), Eclipse Neon Release 64Bit, JNA version 4.2.2. 我的环境如下:Windows 7 Ultimate SP1 64Bit,Jdk和Jvm也是64位(1.8.0_101),Eclipse Neon Release 64Bit,JNA版本4.2.2。

The program stop at Native.free line, and prompt App crash dialog, information is as following: 程序在Native.free行停止,并提示App崩溃对话框,信息如下:

Problem signature:
   Problem Event Name: APPCRASH
   Application name: javaw.exe
   Application version: 8.0.1010.13
   Application timestamp: 576a4c7e
   Fault module name: StackHash_c84f
   Faulty module version: 6.1.7601.18247
   Faulty module timestamp: 521eaf24
   Exception code: c0000374
   Exception offset: 00000000000c4102
   OS version: 6.1.7601.2.1.0.256.1
   Locale ID: 2052
   Additional Information 1: c84f
   Additional information 2: c84f3cec06e628f5bb4621d27c86f80d
   Additional Information 3: 3b2e
   Additional Information 4: 3b2e22e89759af30ea1b1d716fbf08f3

So I wondering if this way to free memory created in dll is wrong? 所以我想知道这种方式来释放在dll中创建的内存是否错误? Can you give me some advice, thanks! 你能给我一些建议吗,谢谢!

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;

public class MemoryTest {

    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary) Native.loadLibrary("c", CLibrary.class);

        Pointer strdup(String src);
    }

    public static void main(String[] args) {
        Pointer p = CLibrary.INSTANCE.strdup("HelloWorld");
        Native.free(Pointer.nativeValue(p));
        System.gc();
    }

}

this test did run on Linux Ubuntu64 16.04 without any crash. 这个测试确实在Linux Ubuntu64 16.04上运行而没有任何崩溃。

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

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