1

I want to write a Java wrapper to a native library (with BridJ). The method I want to wrap takes a struct of operations (callbacks):

typedef struct _operations {
   int (*op1) (int, ...);
   int (*op2) (float, ...);
} operations;

int method(*operations ops);

The operations should obviously be written in Java, so method calls back to Java space (using BridJ's Callback mechanism).

However, after executing the Java callbacks and returning to method, local variables in the C code have changed:

int method(*operations ops) {
  void* uselesspointer;
  DbgPrint("useless pointer before callback is %d\n", uselesspointer); 
  // prints x
  // execute callbacks in Java here
  DbgPrint("useless pointer after callback is %d\n", uselesspointer); 
  // prints something different from x
}

How can I prevent this from happening?

// see https://github.com/Maxhy/dokany/issues/7 for details

// edit thats interesting: the offset of useless pointer seems to be constant, in my application the second debug message is always "192" instead of "0"

Magnilex
  • 11,584
  • 9
  • 62
  • 84
S1lentSt0rm
  • 1,989
  • 2
  • 17
  • 28
  • If you're not initializing the `uselesspointer`, then it will have whatever value happens to be on the stack at that time. Granted, said value should not be changed by the callback execution. – technomage Feb 25 '15 at 22:08
  • @technomage But the debug output from the link I provided tells me something different. Does "should" mean it is impossible to happen? – S1lentSt0rm Feb 25 '15 at 23:17
  • I mean that if the value *is* changed then the callback invocation is not properly preserving the stack. – technomage Feb 26 '15 at 03:32
  • @technomage as I can't really fiddle with the stack in Java, that would be an issue with the java<->native library (BridJ) then, right? – S1lentSt0rm Feb 26 '15 at 08:30
  • Correct. The problem would be either in bridj itself or in the FFI library it is using. I don't know if you can easily hook up a [JNA](https://github.com/twall/jna) version, but that might help isolate the issue. – technomage Feb 26 '15 at 13:09
  • FYI, I'm pretty sure this would work fine with [JavaCPP](https://github.com/bytedeco/javacpp). – Samuel Audet Feb 27 '15 at 02:53

0 Answers0