2

I'm trying to pass object pointer as long from C++ code to java method through JNI to convert it back to pointer in callback later.

void Client::process()
{
    long thisAddress = (long)this;
    QAndroidJniObject res = activity.callObjectMethod("process", "(Ljava/lang/Long;)Ljava/lang/String;", (jlong)thisAddress);
}

Java function prototype is public String process(Long clientAddr) and here is beauty that JVM prints to me: Invalid indirect reference 0x5f3d9bc8 in decodeIndirectRef.

What is wrong with this code? Or, maybe, there is another method to do what I want?

gmlt.A
  • 327
  • 5
  • 16

2 Answers2

2

The signature for a type of jlong is J. Also note that a C++ long has 32 bits, so it is equivalent to a jint and there is no need to use a jlong here. But you can also assign a long to a jlong. It would be converted automatically.

So the method call should be like :

QAndroidJniObject res = activity.callObjectMethod("process", "(J)Ljava/lang/String;", thisAddress);
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • That's another trick: if I use short signature, java method won't call at all. I put simple debug output into it and after such call nothing happens. – gmlt.A Mar 29 '15 at 07:42
2

Ok. I managed to fix my problem by replacing Float to float using short signature "J" instead, as @Nejat suggested.

So now it looks like that Qt:

QAndroidJniObject res = activity.callObjectMethod("process", "(J)Ljava/lang/String;", (jlong)thisAddress);

Java:

public String process(long clientAddr)
gmlt.A
  • 327
  • 5
  • 16