2

I'm creating Java wrappers for some C code using JNI. There are dependencies on the C side that look like this:

a = make_a();
b = make_b(a);

On the Java side I use class A and class B to hold references to the output of make_a() and make_b, along with various operations. Further, class B depends class A sticking around.

How can I create a dependency between the two classes from within JNI?

Update

I need class A to not be GC'd while class B is in use, and I need to create this dependency from within JNI.

Noah Watkins
  • 5,446
  • 3
  • 34
  • 47
  • So you want class B to be able to hold a pointer to class A? I'm not entirely sure what you are getting at, but you can pass a pointer from a C/C++ structure to Java and then back again. – Matt Wonlaw Mar 19 '11 at 16:56
  • I need class A to not be GC'd while class B is in use, and I need to create this dependency from within JNI. – Noah Watkins Mar 19 '11 at 17:51
  • Can't you add to the Java interface (at the point right before it drops into JNI) so calls to make_b(a) will return an instance of B that contains a reference to the passed in A instance? – Matt Wonlaw Mar 20 '11 at 23:00

1 Answers1

1

I think what you want is NewGlobalRef/DeleteGlobalRef. This will let your JNI hang on to an object reference. Be careful-- this is an easy way to create memory leaks as its up to you to delete the reference!

Here are some links to get you going:

Community
  • 1
  • 1
paleozogt
  • 6,393
  • 11
  • 51
  • 94