This Question is Answered

1 "helpful" answer available (2 pts)
3 Replies Last post: Feb 6, 2008 6:23 PM by Serge Piletsky  
Ian   3 posts since
Feb 6, 2008
Currently Being Moderated

Feb 6, 2008 12:51 AM

JNIWrapper callback Exception c0000005, at 00000001

 

My native code successfully executes the Java JniCallback method, and receives the resulting returned data.  The location of the exception is the value of the number parameter being passed to the callback method.  What is wrong?  It does appear that the input value of number is being used as a pointer, but when I try to pass a pointer to number instead of the value, there is no improvement.   Thanks in advance.

 

 

Here is the JniWrapperShim class code:

 

 

 

 

private Library _kernel;

 

    public JniWrapperShim(){

        // add the relative path to the .dll file here

        DefaultLibraryLoader.getInstance().addPath("C:
JNIWrapper
bin");

        kernel = new Library("TestJNIWrapper", Function.CDECLCALLING_CONVENTION);

    }

 

 

    public long TestCallback( ) {

        JniCallback myCallback = new JniCallback();

        int number = 1;

        Int sum = new Int();

        Function testNativeCallback = _kernel.getFunction("nativeCallback");

        testNativeCallback.invoke(sum, new Parameter[]

                                  {

                                      myCallback,

                                      new Int(number)

                                  });

        myCallback.dispose();

        return sum.getValue();

    }

 

 

 

The native TestJniWrapper code:

 

 

typedef int (*Callback) (int);

int sum = 0;

 

__declspec(dllexport) int nativeCallback(Callback func, int number)

{

    cout << "value of sum = " << sum << endl;

    cout << "value of number before callback = " << number << endl;

    sum = func(number);

    cout << "value of sum after callback = " << sum << endl;

    return sum;

}

 

 

 

 

The Java JniCallback class:

 

 

public class JniCallback extends Callback {

 

    private Int argument = new Int();

    private Int res = new Int();

 

    public JniCallback(){

        init(new Parameter[]{argument}, res);

    }

 

    public void callback() {

        res.setValue(argument.getValue()+1);   

    }

 

}

 

 

 

 

The console shows the following:

 

 

value of sum = 0

value of number before callback = 1

value of sum after callback = 2

Exception c0000005, at 00000001

Access violation: attempting to read memory at address 00000001

Native function stack data: 0,858e00,1,2e0072,6c0064,6c,21,a1,4,ffffffff,858c6c,858c7c

 

 

 

 

 

 

Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
1. Feb 6, 2008 3:31 PM in response to: Ian
Re: JNIWrapper callback Exception c0000005, at 00000001

Hi,

 

The cause of the problem in this case is that you call .dispose() method of callback before getting the value of its "sum" variable. So you just need to change the order of operation, for example:

 

   long result = sum.getValue();
   myCallback.dispose();
   return result;

 

-Serge

Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
3. Feb 6, 2008 6:23 PM in response to: Ian
Re: JNIWrapper callback Exception c0000005, at 00000001

Thanks for the update. Please also specify the calling convention of the JniCallback using its setCallingConvention(Function.CDECL_CALLING_CONVENTION) and let me know the results.

 

Example:

   public JniCallback(){
      init(new Parameter[]{argument}, res);
      setCallingConvention(Function.CDECL_CALLING_CONVENTION);
   }

 

-Serge

More Like This

  • Retrieving data ...