JNA: Library function call returns java.lang.Error: Invalid memory access

梦想与她 提交于 2020-01-14 03:34:48

问题


I am working on a java project. In my project i am using java native access(jna) to use a c library pjsip.

I have successfully used some of the function. But when i try to call pj_pool_create() , i get: java.lang.Error: Invalid memory access

I have searched all the related problem published on mailing list, stack overflow, github etc. But I can not get to the solution.

I placed some printf in the source code of the function before compiling it. All the printf statement get printed even the last one which is just before the return statement.
So can someone help me out?

Here is the necessary mappings:
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);
}

Calling the function:

//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

Error!

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)

来源:https://stackoverflow.com/questions/49849222/jna-library-function-call-returns-java-lang-error-invalid-memory-access

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!