前言

接上篇修改linphone-sdk-android-上篇

本文是中篇,本篇记录问题2的后续排查过程及修复方案,尽量描述排查问题过程中的思路与方向

分析

上篇说到增加日志,编译后放到AS中运行,查看Logcat输出

// up不为NULL
2022-04-24 18:10:36.969 4002-4018/com.guodong.android.linphone D/guodongAndroid: up = 0x100433
2022-04-24 18:10:36.969 4002-4018/com.guodong.android.linphone D/guodongAndroid: up_available1 = 0
2022-04-24 18:10:36.969 4002-4018/com.guodong.android.linphone D/guodongAndroid: up_available2 = 0

// up为NULL
2022-04-24 18:10:39.669 4002-4018/com.guodong.android.linphone D/guodongAndroid: up = 0x0
2022-04-24 18:10:39.669 4002-4018/com.guodong.android.linphone D/guodongAndroid: up_available1 = 1
2022-04-24 18:10:39.669 4002-4018/com.guodong.android.linphone D/guodongAndroid: up_available2 = 1

从日志中可以看出,有时upNULL的,猜想有销毁的方法,再次查看linphone_jni.cc,发现有一个unref方法:

JNIEXPORT jboolean JNICALL Java_org_linphone_core_LoggingServiceImpl_unref(JNIEnv* env, jobject thiz, jlong ptr) {
    LinphoneLoggingService *cptr = (LinphoneLoggingService*)ptr;
    if (cptr == 0) {
        bctbx_error("Java_org_linphone_core_LoggingServiceImpl_unref's LinphoneLoggingService C ptr is null!");
        return TRUE;
    }

    jobject wref = (jobject)belle_sip_object_data_get((belle_sip_object_t *)cptr, belle_sip_java_user_data_key);
    belle_sip_object_data_set((belle_sip_object_t *)cptr, belle_sip_java_user_data_key, nullptr, nullptr);
    if (wref) {
        env->DeleteWeakGlobalRef(wref);
    }
    return belle_sip_object_unref_2(cptr) == 1;
}

嗯,看来这个就是销毁方法了,通过belle_sip_object_data_get方法取出值,再通过belle_sip_object_data_set方法设置为nullptr,然后删除全局弱引用

这个方法也加点日志输出吧,打开jni.mustache,找到模板方法,添加日志输出:

JNIEXPORT jboolean JNICALL Java_{{jniPrefix}}{{classImplName}}_unref(JNIEnv* env, jobject thiz, jlong ptr) {
    {{classCName}} *cptr = ({{classCName}}*)ptr;
    if (cptr == 0) {
        bctbx_error("Java_{{jniPrefix}}{{classImplName}}_unref's {{classCName}} C ptr is null!");
        return TRUE;
    }

    jobject wref = (jobject)belle_sip_object_data_get((belle_sip_object_t *)cptr, belle_sip_java_user_data_key);

    // begin - added
    {{#isLoggingService}}
    #ifdef __ANDROID__
    __android_log_print(ANDROID_LOG_DEBUG, "guodongAndroid", "unref wref = %p", wref);
    #endif /* __ANDROID__ */
    {{/isLoggingService}}
      // end - added

    belle_sip_object_data_set((belle_sip_object_t *)cptr, belle_sip_java_user_data_key, nullptr, nullptr);
    if (wref) {
        env->DeleteWeakGlobalRef(wref);
    }
    {{#refCountable}}return belle_sip_object_unref_2(cptr) == 1;{{/refCountable}}
    {{#notRefCountable}}return FALSE;{{/notRefCountable}}
}

重新编译后放到AS中运行,查看Logcat输出

2022-04-24 18:18:52.359 4240-4249/com.guodong.android.linphone D/guodongAndroid: unref wref = 0x1002e3
2022-04-24 18:19:02.296 4240-4257/com.guodong.android.linphone D/guodongAndroid: up = 0x0
2022-04-24 18:19:02.296 4240-4257/com.guodong.android.linphone D/guodongAndroid: up_available1 = 1
2022-04-24 18:19:02.296 4240-4257/com.guodong.android.linphone D/guodongAndroid: up_available2 = 1

从日志中可以分析出两点:

  1. 的确有销毁的方法被调用
  2. 调用unref方法的线程与调用getLoggingService方法的线程不同

结合以上两点,大胆的猜测问题出在多线程上,在多线程上此问题是偶现的就不奇怪了

出问题时,日志输出如下

2022-04-24 18:25:02.296 4240-4257/com.guodong.android.linphone D/guodongAndroid: up = 0x20004f
2022-04-24 18:25:02.296 4240-4257/com.guodong.android.linphone D/guodongAndroid: up_available1 = 1
2022-04-24 18:25:02.296 4240-4249/com.guodong.android.linphone D/guodongAndroid: unref wref = 0x1002e3
App Crash

多线程问题,第一想法是通过加锁,保证代码间调用的互斥性

再次打开jni.mustache,添加互斥锁相关代码:

// Added by guodongAndroid on 2022/04/22
#ifdef __ANDROID__
static pthread_mutex_t mutex;
#endif /* __ANDROID__ */

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *ajvm, void *reserved) {
#ifdef __ANDROID__
    ms_set_jvm(ajvm);
    int result = pthread_mutex_init(&mutex, NULL);
    __android_log_print(ANDROID_LOG_DEBUG, "guodongAndroid", "JNI_OnLoad, mutex init result = %d", result);
#endif /* __ANDROID__ */
    jvm = ajvm;
    return JNI_VERSION_1_2;
}

// Added by guodongAndroid on 2022/04/22
JNIEXPORT void JNI_OnUnload(JavaVM *ajvm, void *reserved) {
#ifdef __ANDROID__
    int result = pthread_mutex_destroy(&mutex);
    __android_log_print(ANDROID_LOG_DEBUG, "guodongAndroid", "JNI_OnUnload, mutex destroy result = %d", result);
#endif /* __ANDROID__ */
}

{{#objects}}
JNIEXPORT jobject JNICALL get{{className}}(JNIEnv *env, {{classCName}} *cptr, bool_t takeref) {
    jobject jobj = nullptr;

    if (cptr != nullptr) {
        // begin add
        {{#isLoggingService}}
        #ifdef __ANDROID__
        pthread_mutex_lock(&mutex);
        #endif /* __ANDROID__ */
        {{/isLoggingService}}
        // end add
        
        void *up = belle_sip_object_data_get((belle_sip_object_t *)cptr, belle_sip_java_user_data_key);
        LinphoneJavaBindings *ljb = (LinphoneJavaBindings *)linphone_factory_get_user_data(linphone_factory_get());
        if (!ljb) {
            ljb = new LinphoneJavaBindings(env);
            linphone_factory_set_user_data(linphone_factory_get(), ljb);
        }

        jclass {{cPrefix}}_class = ljb->{{cPrefix}}_class;
        jmethodID {{cPrefix}}_constructor = ljb->{{cPrefix}}_class_constructor;

        {{#isLoggingService}}
        #ifdef __ANDROID__
        __android_log_print(ANDROID_LOG_DEBUG, "guodongAndroid", "up = %p", up);
        
        jobject temp_jobj1 = (jobject)up;
        jboolean up_available1 = env->IsSameObject(temp_jobj1, NULL);
        __android_log_print(ANDROID_LOG_DEBUG, "guodongAndroid", "up_available1 = %d", up_available1);
        
        jobject temp_jobj2 = (jobject)up;
        jboolean up_available2 = env->IsSameObject(temp_jobj2, nullptr);
        __android_log_print(ANDROID_LOG_DEBUG, "guodongAndroid", "up_available2 = %d", up_available2);
        #endif /* __ANDROID__ */
        {{/isLoggingService}}

        if (up == nullptr) {
            jobj = env->NewObject({{cPrefix}}_class, {{cPrefix}}_constructor, (jlong)cptr);
            belle_sip_object_data_set((belle_sip_object_t *)cptr, belle_sip_java_user_data_key, (void*)env->NewWeakGlobalRef(jobj), nullptr);
            if (takeref)
                {{#refCountable}}{{cPrefix}}_ref(cptr);{{/refCountable}}
        } else {
            jobj = env->NewLocalRef((jobject)up);
            if (jobj == nullptr) {
                // Delete weak ref ?
                env->DeleteWeakGlobalRef((jobject)up);
                // takes implicit local ref
                jobj = env->NewObject({{cPrefix}}_class, {{cPrefix}}_constructor, (jlong)cptr);
                belle_sip_object_data_set((belle_sip_object_t *)cptr, belle_sip_java_user_data_key, (void*)env->NewWeakGlobalRef(jobj), nullptr);
                if (takeref)
                    {{#refCountable}}{{cPrefix}}_ref(cptr);{{/refCountable}}
            }
        }
        
        // begin add
        {{#isLoggingService}}
        pthread_mutex_unlock(&mutex);
        {{/isLoggingService}}
        // end add
    }
    return jobj;
}

JNIEXPORT jboolean JNICALL Java_{{jniPrefix}}{{classImplName}}_unref(JNIEnv* env, jobject thiz, jlong ptr) {
    {{classCName}} *cptr = ({{classCName}}*)ptr;
    if (cptr == 0) {
        bctbx_error("Java_{{jniPrefix}}{{classImplName}}_unref's {{classCName}} C ptr is null!");
        return TRUE;
    }

    // begin add
    {{#isLoggingService}}
    #ifdef __ANDROID__
    pthread_mutex_lock(&mutex);
    #endif /* __ANDROID__ */
    {{/isLoggingService}}
    // end add
    
    jobject wref = (jobject)belle_sip_object_data_get((belle_sip_object_t *)cptr, belle_sip_java_user_data_key);

    {{#isLoggingService}}
    #ifdef __ANDROID__
    __android_log_print(ANDROID_LOG_DEBUG, "guodongAndroid", "unref wref = %p", wref);
    #endif /* __ANDROID__ */
    {{/isLoggingService}}

    belle_sip_object_data_set((belle_sip_object_t *)cptr, belle_sip_java_user_data_key, nullptr, nullptr);
    if (wref) {
        env->DeleteWeakGlobalRef(wref);
    }
    
    // begin add
    {{#isLoggingService}}
    pthread_mutex_unlock(&mutex);
    {{/isLoggingService}}
    // end add
    
    {{#refCountable}}return belle_sip_object_unref_2(cptr) == 1;{{/refCountable}}
    {{#notRefCountable}}return FALSE;{{/notRefCountable}}
}

重新编译后拷贝到AS中运行,持续观察Logcat及运行情况


guodongAndroid
1 声望1 粉丝

始于Android,不止于Android