Igor
Igor

Reputation: 124

Android JNI: SIGSEGV on CallObjectMethod

I am trying to call method of custom Java type, but receiving SIGSEGV signal.

Here is the Java Code

package com.example.JniApi;
public class JniApi {   
    public void test()
    {
        MyType obj = new MyType();
        nativePrintMyTypeMsg(obj);
    }
    public static native void nativePrintMyTypeMsg(MyType obj);
}

public class MyType {
    public void printMsg()
    {
        android.util.Log.d("TestJni", "From Java");
    }
}

And here is the native code

JNIEXPORT void JNICALL Java_com_example_JniApi_JniApi_nativePrintMyTypeMsg
(JNIEnv *env , jclass, jobject myObj)
{
    jclass myTypeClass = env->GetObjectClass(myObj);
    jmethodID printMsgMethodId = env->GetMethodID(myTypeClass,"printMsg", "()V");
    for(size_t i = 0; i < 1000; ++i){
        env->CallObjectMethod(myObj, printMsgMethodId);
    }
    return;
}

Upvotes: 0

Views: 2047

Answers (1)

Naytzyrhc
Naytzyrhc

Reputation: 2317

just from memory and looking at your snippet, there are 2 things I'd like to point out:

(JNIEnv *env , jclass, jobject myObj)

This signature looks a bit odd with the second parameter missing a parameter object. For sanity please change it to

(JNIEnv *env , jclass jclazz, jobject myObj)

Next and probably more important, the method printMsg() in your MyType-class is actually returning void. To invoke a void-method via JNI, you need to call it via CallVoidMethod instead. For more information, please check the JNI Documentation

Upvotes: 1

Related Questions