本文介绍: 前言接上篇修改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.g
前言
本文是中篇,本篇记录问题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
从日志中可以看出,有时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}}
}
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
结合以上两点,大胆的猜测问题出在多线程上,在多线程上此问题是偶现的就不奇怪了
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
// 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}}
}
原文地址:https://blog.csdn.net/guodongAndroid/article/details/124433750
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_30134.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。