JNA:库函数调用返回java.lang.Error:无效的内存访问

时间:2018-04-16 03:23:35

标签: java jna pjsip

我正在研究一个java项目。在我的项目中,我使用java本机访问(jna)来使用c库pjsip

我已经成功使用了一些功能。但是当我尝试拨打pj_pool_create()时,我得到:java.lang.Error: Invalid memory access

我搜索了在邮件列表,堆栈溢出,github等上发布的所有相关问题但是我无法找到解决方案。

在编译之前,我在函数的源代码中放了一些printf。所有的printf语句都会被打印出来,即使是在return语句之前的最后一个语句 那么有人可以帮助我吗?

这是必要的映射:
function(plz click and hava a look)

pj_pool_t * pj_pool_create (pj_pool_factory *factory, const char    
*name, pj_size_t initial_size, pj_size_t increment_size, 
pj_pool_callback *callback)

public pj_pool_t pj_pool_create( pj_pool_factory factory,String  
name,NativeLong initial_size,NativeLong increment_size,pj_pool_callback  
callback);

pj_pool_t:

public static class pj_pool_t extends Structure {

    public static class ByReference extends pj_pool_t implements Structure.ByReference {}
    public static class ByValue extends pj_pool_t implements Structure.ByValue { }

    public pj_pool_t.ByReference prev;
    public pj_pool_t.ByReference next;
    public byte[] obj_name = new byte[32];
    public pj_pool_factory.ByReference factory;
    public Pointer factory_data;
    public NativeLong capacity;
    public NativeLong increment_size;
    public pj_pool_block block_list;
    public pj_pool_callback callback;
    // public  pj_pool_mem.ByReference first_mem; // There is no such field in the source code
    // public NativeLong used_size; // There is no such field in the source code
    // public pj_pool_callback cb; // There is not such field in the source code

    @Override
    protected List<String> getFieldOrder() {
        // TODO Auto-generated method stub
        return Arrays.asList(new String[] {"prev","next","obj_name","factory","factory_data","capacity","increment_size","block_list","callback"});
    }

}

pj_pool_factory:

public static class pj_pool_factory extends Structure {

    public static class ByReference extends pj_pool_factory implements Structure.ByReference {
        public ByReference() {}
        public ByReference(Pointer p) { super(p); read();}
    }
    public static class ByValue extends pj_pool_factory implements Structure.ByValue{}

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

    public static interface create_pool extends Callback {
        public pj_pool_t invoke(pj_pool_factory factory,String name,NativeLong  initial_size, NativeLong increment_size, pj_pool_callback callback);   
    }

    public static interface release_pool extends Callback {
        public void invoke(pj_pool_factory factory,pj_pool_t pool);    
    }
    public static interface dump_status extends Callback {
        public void invoke(pj_pool_factory factory, int detail);    
    }
    public static interface on_block_alloc extends Callback {
        public int invoke(pj_pool_factory factory, NativeLong size);    
    }
    public static interface on_block_free extends Callback {
        public void invoke(pj_pool_factory factory, NativeLong size);    
    }

    public pj_pool_factory_policy policy;
    public create_pool create_pool;
    public release_pool release_pool;
    public dump_status dump_status;
    public on_block_alloc on_block_alloc;
    public on_block_free on_block_free;
    //public int dummy;

    @Override
    protected List<String> getFieldOrder() {
        // TODO Auto-generated method stub
        return Arrays.asList(new String[] { "policy","create_pool","release_pool","dump_status","on_block_alloc","on_block_free"});
    }

}

pj_pool_callback

public interface pj_pool_callback extends Callback {
    void invoke(pj_pool_t pool, NativeLong size);
}

调用该函数:

//creating an empty pool
Test.pj_pool_t pool = new Test.pj_pool_t();

//creating an empty caching_pool
Test.pj_caching_pool cp = new Test.pj_caching_pool();

//parameter for the function call. long -> NativeLong
NativeLong zero = new NativeLong(0,true);

//initialize caching_pool. This get initialized properly
pjlib.pj_caching_pool_init(cp, null, zero);

//initialized pool_factory
Test.pj_pool_factory pf = cp.factory;   

//long parameter for function call
NativeLong thousands = new NativeLong(1000,true);

//finally call the library function. but error occurs here
pool = pjlib.pj_pool_create( pf , "server", thousands, thousands, null);// null is ok for the last parameter
//pool = pjlib.pj_pool_create( pf , "server", 1000, 1000, null); tried this also

错误!

Exception in thread "Thread-0" java.lang.Error: Invalid memory access
at com.sun.jna.Native._getPointer(Native Method)
at com.sun.jna.Native.getPointer(Native.java:2138)
at com.sun.jna.Pointer.getPointer(Pointer.java:651)
at com.sun.jna.Pointer.getValue(Pointer.java:376)
at com.sun.jna.Structure.readField(Structure.java:720)
at com.sun.jna.Structure.read(Structure.java:580)
at com.sun.jna.Structure.autoRead(Structure.java:2074)
at com.sun.jna.Structure.conditionalAutoRead(Structure.java:550)
at com.sun.jna.Structure.updateStructureByReference(Structure.java:678)
at com.sun.jna.Pointer.getValue(Pointer.java:376)

0 个答案:

没有答案