带有JNA结构的内存访问错误,该结构包含指向指针数组的指针

时间:2018-01-29 14:19:49

标签: pointers constructor jna

以下是我正在使用的代码: C API:

typedef struct {
    int matId;
    int nMatNameSize;
    char* materialName;
    int isHyMat;
    lDVDetailOutput* dVOutputs;
} innerStruct;

typedef struct {
    int nMaterials;
    innerStruct** moreDetailOutputs;
} detailOutput;

int LDValues(const char* version,
             const detailInput* input,
             detailOutput* output);

JNA声明:

public static class lDVDetailOutput extends Structure {
    public static class ByReference extends lDVDetailOutput implements Structure.ByReference{}

    int dValue;
    int nVars;
    yetAnotherStructure.ByReference vars;

    @Override
    protected List<String> getFieldOrder() {
        return Arrays.asList(new String[] { 
            "dValue", "nVars", "yetAnotherStructure", });
    }
}

public static class jInnerStruct extends Structure{
    int matId;
    int nMatNameSize;
    String materialName;
    int isHyMat;
    lDVDetailOutput.ByReference dValueOutputs;

    jInnerStruct() {
        super();
    }

    @Override
    protected List<String> getFieldOrder() {
        return Arrays.asList(new String[] { 
            "matId", "nMatNameSize", "materialName",
            "isHyMat", "dValueOutputs", });
    }
}

public static class jDetailOutput extends Structure {
    public static class ByReference extends detailOutput implements Structure.ByReference{}

    int nMaterials;
    Pointer moreDetailOutputs; // jInnerStruct**

    public jDetailOutput(Pointer p) {
        super(p);
        read();
    }

    @Override
    protected List<String> getFieldOrder() {
        return Arrays.asList(new String[] { 
            "nMaterials", "moreDetailOutputs", });
    }
}

public static LDValues(final String inputString,
                       final jDetailInput.ByReference inputValues,
                       jDetailOutput.ByReference outputValues) {}

在我的主程序中,我有以下代码:

jDetailOutput.ByReference recvDetailOutput = new jDetailOutput.ByReference();

int success = LDValues(stringObject, inputValues, recvDetailOutput);

Pointer[] myDetailOutputPointers = recvDetailOutput.moreDetailOutputs.getPointerArray(0, recvDetailOutput.detailOutput.nMaterials);

jInnerStruct interimLMDO = new jInnerStruct(matDetailOutputPointers[0]);

我在jDetailOutput(Pointer P)构造函数中的read()调用期间遇到内存访问错误。特别是在尝试读取jInnerStruct的dValueOutputs成员时。

使用Eclipse调试器我看到dValueOutputs的值为null。我确实看到jDetailOutput中的nMaterials中有正确的值。

我是否需要为jInnerStruct创建不同的构造函数?

谢谢, 道格

0 个答案:

没有答案