下面是c头文件:
idevice_error_t idevice_get_device_list(char ***devices, int *count);
idevice_error_t idevice_device_list_free(char **devices);这是JNAerator为我生成的JNA:
int idevice_get_device_list(PointerByReference devices, IntBuffer count);
int idevice_device_list_free(PointerByReference devices);下面是我使用它的方法。它是用Kotlin编写的,但它也等同于在Java中执行的操作:
fun getDeviceList(): List<String> {
val deviceUuidsPbr = PointerByReference()
val deviceSizeBuffer = IntBuffer.wrap(IntArray(1))
val resultInt = LibIMobileDeviceLibrary.INSTANCE.idevice_get_device_list(deviceUuidsPbr, deviceSizeBuffer)
val size = deviceSizeBuffer.get()
logger.v {"getDeviceList $resultInt, Size: $size" }
val stringArrayP = deviceUuidsPbr.value
val devices = stringArrayP
.getStringArray(0, size)
.toList()
logger.v { "Devices: $devices" }
LibIMobileDeviceLibrary.INSTANCE.idevice_device_list_free(deviceUuidsPbr)
return devices
}当我释放内存时,所有东西都会爆炸:
LibIMobileDeviceLibrary.INSTANCE.idevice_device_list_free(deviceUuidsPbr)free想要一个char **devices,但我提交了一个char ***devices。如何将PointerByReference转换为正确的格式?
发布于 2021-06-06 07:39:06
PointerByReference只是一个指针(指向一个指针)。被映射的两个C函数只有一个额外的间接层。
考虑下面的函数定义:
device_get_device_list(char ***devices, int *count);
idevice_device_list_free(char **devices);现在考虑将foo定义为*devices,并将原始函数简化为:
device_get_device_list(char **foo, int *count);
idevice_device_list_free(char *foo);如果你将bar定义为*foo,你会得到:
device_get_device_list(char *bar, int *count);
idevice_device_list_free(char bar);因此,您不能将从device_get_device_list (*bar)接收的PointerByReference直接传递给idevice_device_list_free (它需要bar);您需要传递其指向的值(恰好是指向另一个指针的指针,但这并不重要)。
在Java语言中,只需将idevice_device_list_free()调用中的参数从deviceUuidsPbr更改为deviceUuidsPbr.getValue()即可。
我不是一个Kotlin用户,但它似乎基于您的其他代码,您需要deviceUuidsPbr.value那里。
https://stackoverflow.com/questions/67852323
复制相似问题