[英]Jna, random Invalid Memory Access error
I have an openvr binding which has a minor problem since a while 我有一个openvr绑定 ,自一段时间以来有一个小问题
Essentially, from time to time, whenever I free the memory of some geometric 3d model (basestation or controller) I get the Error
: 基本上,每当我释放一些几何3D模型(基站或控制器)的内存时,都会收到
Error
:
"java.lang.Error: Invalid memory access"
“ java.lang.Error:无效的内存访问”
This is the test I'm using so far, it loads and free x times a list of models present in the Steam installation folder: 到目前为止,这是我正在使用的测试 ,它将加载并释放x倍于Steam安装文件夹中存在的模型的列表:
for (i in 0..99) models.forEach(::loadRenderModel)
fun loadRenderModel(renderModelName: String) {
var error: EVRRenderModelError
val rm = vrRenderModels!!
val pModel = PointerByReference()
while (true) {
error = rm.loadRenderModel_Async(renderModelName, pModel)
if (error != EVRRenderModelError.Loading) break
Thread.sleep(1)
}
val model = RenderModel.ByReference(pModel.value)
if (error != EVRRenderModelError.None) {
System.err.println("Unable to load render model $renderModelName - ${error.getName()}")
return // move on to the next tracked device
}
try {
rm freeRenderModel model
} catch (e: Error) {
System.err.println(e)
}
println()
}
RenderModel
struct in C++ : C ++中的
RenderModel
结构:
struct RenderModel_t
{
const RenderModel_Vertex_t *rVertexData; // Vertex data for the mesh
uint32_t unVertexCount; // Number of vertices in the vertex data
const uint16_t *rIndexData; // Indices into the vertex data for each triangle
uint32_t unTriangleCount; // Number of triangles in the mesh. Index count is 3 * TriangleCount
TextureID_t diffuseTextureId; // Session unique texture identifier. Rendermodels which share the same texture will have the same id. <0 == texture not present
};
open class RenderModel : Structure {
/** Vertex data for the mesh */
@JvmField
var rVertexData: RenderModel_Vertex.ByReference? = null
/** Number of vertices in the vertex data */
@JvmField
var vertexCount = 0
/** Indices into the vertex data for each triangle */
@JvmField
var rIndexData: ShortByReference? = null
/** Number of triangles in the mesh. Index count is 3 * TriangleCount */
@JvmField
var triangleCount = 0
/** Session unique texture identifier. Rendermodels which share the same texture will have the same id. <0 == texture not present */
@JvmField
var diffuseTextureId = INVALID_TEXTURE_ID
val vertices
get() = rVertexData?.pointer?.getByteArray(0, vertexCount * RenderModel_Vertex.SIZE)
val indices
get() = rIndexData?.pointer?.getByteArray(0, triangleCount * 3 * Short.BYTES)
constructor()
constructor(vertexData: RenderModel_Vertex.ByReference, vertexCount: Int, indexData: ShortByReference, triangleCount: Int,
diffuseTextureId: Int) {
this.rVertexData = vertexData
this.vertexCount = vertexCount
this.rIndexData = indexData
this.triangleCount = triangleCount
this.diffuseTextureId = diffuseTextureId
}
override fun getFieldOrder()= listOf("rVertexData", "vertexCount", "rIndexData",
"triangleCount", "diffuseTextureId")
constructor(peer: Pointer) : super(peer) {
read()
}
class ByReference : RenderModel, Structure.ByReference {
constructor() : super()
constructor(peer: Pointer) : super(peer)
}
class ByValue : RenderModel(), Structure.ByValue
}
I tend to believe everything so far (regarding class definition) is correct since I do read valid values. 我倾向于相信到目前为止(关于类定义)的一切都是正确的,因为我确实读取了有效值。
However I keep getting that error from time to time, I checked some pointer values and I didn't spot any weird value 但是我不时收到该错误,我检查了一些指针值,但没有发现任何奇怪的值
What can it be? 会是什么
I found a bug in your code, but I can't confirm it is the reason of your failures. 我在您的代码中发现了一个错误,但是我无法确认这是您失败的原因。 It definitely could produce them.
它肯定可以生产它们。
You say 你说
val error = EVRRenderModelError.None
and then 接着
if (error != EVRRenderModelError.None) {
which is a constantly false
condition. 这是一个不断
false
状况。 Normally there's an IntelliJ inspection that warns you about these errors, but it could be disabled in your config. 通常,有IntelliJ检查会警告您这些错误,但是可以在您的配置中将其禁用。
You should use a var error
instead and reassign it within the busy-wait loop of the async call. 您应该改用
var error
,然后在异步调用的busy-wait循环中重新分配它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.