Java JNA在共享对象dll中创建的可用内存
我尝试了libvirt java绑定,当库尝试使用Native.free方法释放dll中分配的内存时,总是出现访问冲突错误,因此我编写了以下简单测试Java JNA在共享对象dll中创建的可用内存,java,c++,eclipse,jna,Java,C++,Eclipse,Jna,我尝试了libvirt java绑定,当库尝试使用Native.free方法释放dll中分配的内存时,总是出现访问冲突错误,因此我编写了以下简单测试 public class HelloWorld { public interface CLibrary extends Library { CLibrary INSTANCE = (CLibrary) Native.loadLibrary("msvcrt", CLibrary.class); Pointe
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));
}
}
我的环境如下:
Windows 7 Ultimate SP1 64位,Jdk和Jvm也是64位(1.8.0_101),Eclipse Neon版本为64位,JNA版本为4.2.2
程序在Native.free行停止,并提示“应用程序崩溃”对话框,信息如下:
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
所以我想知道这种释放dll中创建的内存的方法是否是错误的?你能给我一些建议吗,谢谢
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();
}
}
此测试确实在Linux Ubuntu64 16.04上运行,没有发生任何崩溃
此测试确实在Linux Ubuntu64 16.04上运行,没有发生任何崩溃。为什么不尝试向界面添加一个
免费的定义并使用它呢?它可能不会修复您的崩溃,但您可以直接传递指针
,而不是提取整数值作为参数。是的,我现在使用此方法释放内存,但我不知道它是否正确,程序不再崩溃了。为什么不尝试在界面中添加一个免费的定义并使用它呢?它可能不会修复您的崩溃,但您可以直接传递指针
,而不是提取整数值作为参数。是的,我现在使用此方法释放内存,但我不知道它是否正确,程序不再崩溃。