前言
接上篇批改linphone-sdk-android-上篇
本文是中篇,本篇记录问题2的后续排查过程及修复计划,尽量形容排查问题过程中的思路与方向
剖析
上篇说到减少日志,编译后放到AS中运行,查看Logcat输入
// up不为NULL2022-04-24 18:10:36.969 4002-4018/com.guodong.android.linphone D/guodongAndroid: up = 0x1004332022-04-24 18:10:36.969 4002-4018/com.guodong.android.linphone D/guodongAndroid: up_available1 = 02022-04-24 18:10:36.969 4002-4018/com.guodong.android.linphone D/guodongAndroid: up_available2 = 0// up为NULL2022-04-24 18:10:39.669 4002-4018/com.guodong.android.linphone D/guodongAndroid: up = 0x02022-04-24 18:10:39.669 4002-4018/com.guodong.android.linphone D/guodongAndroid: up_available1 = 12022-04-24 18:10:39.669 4002-4018/com.guodong.android.linphone D/guodongAndroid: up_available2 = 1
从日志中能够看出,有时up
是NULL
的,猜测有销毁的办法,再次查看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 = 0x1002e32022-04-24 18:19:02.296 4240-4257/com.guodong.android.linphone D/guodongAndroid: up = 0x02022-04-24 18:19:02.296 4240-4257/com.guodong.android.linphone D/guodongAndroid: up_available1 = 12022-04-24 18:19:02.296 4240-4257/com.guodong.android.linphone D/guodongAndroid: up_available2 = 1
从日志中能够剖析出两点:
- 确实有销毁的办法被调用
- 调用
unref
办法的线程与调用getLoggingService
办法的线程不同
联合以上两点,大胆的猜想问题出在多线程上,在多线程上此问题是偶现的就不奇怪了
出问题时,日志输入如下
2022-04-24 18:25:02.296 4240-4257/com.guodong.android.linphone D/guodongAndroid: up = 0x20004f2022-04-24 18:25:02.296 4240-4257/com.guodong.android.linphone D/guodongAndroid: up_available1 = 12022-04-24 18:25:02.296 4240-4249/com.guodong.android.linphone D/guodongAndroid: unref wref = 0x1002e3App 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/22JNIEXPORT 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及运行状况