Search code examples
android-studiojava-native-interface

Watching pointers in Android Studio JNI


I am puzzled with strange issue: when I step into JNI function android studio 3.0.1 debugger shows completely wrong values in Variables view for pointers (including "this" pointer)

#include <jni.h>

class T
{
public:
    void a4(T* p, long pp)
    {
        T *_p = (T *) pp;
        bool b = p == _p;
        bool b1 = this == p;
        bool b2 = this == _p;
    }
};

extern "C" JNIEXPORT jstring JNICALL
Java_com_simplesvc_myapplication_MainActivity_stringFromJNI(JNIEnv *env, jobject /* this */)
{
    T *p = new T();
    long pp = (long) p;
    p->a4(p, pp);

    return env->NewStringUTF("hello");
}

before stepping into:

before

after:

after

as you see on screenshot value of "this" is wrong, and values of p parameter changes after entering into a4(). interesting that passing value as long (pp parameter) works ok. it looks like android studio convert pointerts in Variables view for some reason

any ideas how to watch right values for pointers parameters?


Solution

  • looks like I found the way to fix it: you have to use 64-bit image in emulator and compile for x86_64

    android {
        ndk {
            abiFilters 'x86_64'
        }
    }