Artem
Artem

Reputation: 1566

Android ndk Fatal signal code=1

I'm trying to call Java method from c++.

C++

JNIEXPORT void JNICALL Java_ru_sploid_platerecog_RecogActivity_FindFeatures(JNIEnv* env, jobject job, jlong addr_rgba)
{
Mat& m_rgba = *(Mat*)addr_rgba;

try
{
    const pair< string, int > fn = read_number( m_rgba, 10 );
    jclass clazz = env->FindClass("ru/sploid/platerecog/RecogActivity");
    jmethodID meth=env->GetMethodID(clazz,"onGetNumber","(Ljava/lang/String;)V");
    env->CallVoidMethod(job,meth,fn.first.data());
//  cv::putText( m_rgba, fn.first.empty() ? string( "not found" ) : fn.first, cv::Point( 20, 100 ), CV_FONT_HERSHEY_PLAIN, 2.0, cv::Scalar( 255, 0, 0, 0 ) );
}
catch ( const std::exception& e )
{
    cout << "Catch exception: " << e.what() << endl;
    cv::putText( m_rgba, "Exception", cv::Point( 20, 100 ), CV_FONT_HERSHEY_PLAIN, 2.0, cv::Scalar( 255, 0, 0, 0 ) );

}
}

Java:

public void onGetNumber(String plate){
    plat=plate;
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if (plat!=null)
                Toast.makeText(getApplicationContext(), plat, Toast.LENGTH_LONG).show();
            else
                Toast.makeText(getApplicationContext(), "((", Toast.LENGTH_LONG).show();
        }
    });

}

And app closes with 03-12 23:33:29.172: A/libc(21987): Fatal signal 11 (SIGSEGV) at 0x323030b9 (code=1)

I think that I have error in C++. Thank you

Upvotes: 0

Views: 859

Answers (2)

Alex Cohn
Alex Cohn

Reputation: 57163

You don't have validation in your code. I hope this was cut out for this post. You should check that clazz and method are valid.

Anyways, if I am not missing something, you pass a char* to the Java method instead of jstring. You must convert fn.first to Java string with JNI NewStringUTF() or similar.

Upvotes: 1

Seva Alekseyev
Seva Alekseyev

Reputation: 61380

Looks like you are passing a char* to CallVoidMethod() - that's wrong, you need to pass a jstring object. To make a jstring around a char*, use env->NewStringUTF().

In other words, the line goes like this:

env->CallVoidMethod(job,meth,env->NewStringUTF(fn.first.data()));

Assuming the string is indeed in UTF-8. If it's in another codepage (e. g. CP1251) and may contain non-ASCII characters, you need to convert.

Upvotes: 2

Related Questions