I am trying to use the android AccountManager from qt c++ code. To add a account, I want to create an instance of android.accounts.Account, I am trying to do this with this code:
jstring jUsername = QAndroidJniObject::fromString(username).object<jstring>();
jstring jPassword = QAndroidJniObject::fromString(password).object<jstring>();
jstring jType = QAndroidJniObject::fromString(type).object<jstring>();
qDebug()<<"Creating";
QAndroidJniObject accountObject("android.accounts.Account","(Ljava/lang/String;Ljava/lang/String;)V",jUsername,jType);
qDebug()<<"Inserting";
The code segfaults at the line, where the accountObject is created ("Creating" is printed, "Inserting" not):
JNI ERROR (app bug): accessed deleted global reference 0x100e46
JNI ERROR (app bug): accessed deleted global reference 0xe46
I read this occurs, if I call a method with a wrong signature, but the signature is right (see here).
By the looks of it, the way you're creating your strings are causing your problem.
What this does is create an anonymous temporary
QAndroidJniObject(returned byfromString), which you then extract the wrappedjobjectfrom (and cast it to ajstring). By the time execution of that statement finishes the lifetime of thatQAndroidJniObjectis over, and the reference it held to the wrappedjobjectwill be released.You could change your code to somethine like this:
Or to:
Assuming that you have a way of getting the
JNIEnv*. If you create a new reference you should probably also delete it withDeleteLocalRefwhen you don't need it anymore.