问题
I'm building a SIP Softphone using PJSIP for android based on SWIG example.
I enable log as described in the sample:
LogConfig log_cfg = epConfig.getLogConfig();
SipLogWriter logWriter = new SipLogWriter();
log_cfg.setWriter(logWriter);
log_cfg.setDecor(log_cfg.getDecor() & ~(pj_log_decoration.PJ_LOG_HAS_CR.swigValue() | pj_log_decoration.PJ_LOG_HAS_NEWLINE.swigValue()));
where my SipLogWriter class is:
public class SipLogWriter extends LogWriter {
@Override
public void write(LogEntry entry) {
Log.d(Global.TAG, "[SipLogWriter] " + entry.getMsg());
}
}
Pjsip lib halts every now and then with this backtraces:
05-04 10:32:10.534 I/DEBUG ( 281): backtrace:
05-04 10:32:10.534 I/DEBUG ( 281): #00 pc 00037b58 /system/lib/libc.so (tgkill+12)
05-04 10:32:10.544 I/DEBUG ( 281): #01 pc 00013fc9 /system/lib/libc.so (pthread_kill+52)
05-04 10:32:10.544 I/DEBUG ( 281): #02 pc 00014be7 /system/lib/libc.so (raise+10)
05-04 10:32:10.544 I/DEBUG ( 281): #03 pc 00011529 /system/lib/libc.so (__libc_android_abort+36)
05-04 10:32:10.544 I/DEBUG ( 281): #04 pc 0000fcb4 /system/lib/libc.so (abort+4)
05-04 10:32:10.544 I/DEBUG ( 281): #05 pc 00001259 /system/lib/libstdc++.so
05-04 10:32:10.544 I/DEBUG ( 281): #06 pc 00000afb /system/lib/libstdc++.so (__cxa_pure_virtual+6)
05-04 10:32:10.544 I/DEBUG ( 281): #07 pc 0015d4e8 /data/app/com.sample.app-1/lib/arm/libpjsua2.so (pj::Endpoint::utilLogWrite(pj::LogEntry&)+180)
05-04 10:32:10.544 I/DEBUG ( 281): #08 pc 0015d734 /data/app/com.sample.app-1/lib/arm/libpjsua2.so (pj::Endpoint::logFunc(int, char const*, int)+292)
05-04 10:32:10.544 I/DEBUG ( 281): #09 pc 001a9fe0 /data/app/com.sample.app-1/lib/arm/libpjsua2.so (log_writer+164)
05-04 10:32:10.544 I/DEBUG ( 281): #10 pc 0034420c /data/app/com.sample.app-1/lib/arm/libpjsua2.so (pj_log+2324)
05-04 10:32:10.544 I/DEBUG ( 281): #11 pc 0034436c /data/app/com.sample.app-1/lib/arm/libpjsua2.so (pj_log_4+44)
05-04 10:32:10.544 I/DEBUG ( 281): #12 pc 0025c9c4 /data/app/com.sample.app-1/lib/arm/libpjsua2.so (play_cb+568)
05-04 10:32:10.544 I/DEBUG ( 281): #13 pc 0029c454 /data/app/com.sample.app-1/lib/arm/libpjsua2.so (bqPlayerCallback+420)
05-04 10:32:10.544 I/DEBUG ( 281): #14 pc 00008daf /system/lib/libwilhelm.so
05-04 10:32:10.544 I/DEBUG ( 281): #15 pc 000559fd /system/lib/libmedia.so (android::AudioTrack::processAudioBuffer()+1156)
05-04 10:32:10.544 I/DEBUG ( 281): #16 pc 00055c69 /system/lib/libmedia.so (android::AudioTrack::AudioTrackThread::threadLoop()+168)
05-04 10:32:10.544 I/DEBUG ( 281): #17 pc 0000ef55 /system/lib/libutils.so (android::Thread::_threadLoop(void*)+112)
05-04 10:32:10.544 I/DEBUG ( 281): #18 pc 0000eac5 /system/lib/libutils.so
05-04 10:32:10.544 I/DEBUG ( 281): #19 pc 000137b3 /system/lib/libc.so (__pthread_start(void*)+30)
05-04 10:32:10.544 I/DEBUG ( 281): #20 pc 00011893 /system/lib/libc.so (__start_thread+6)
I suspected that it's trying to write logs from the wrong thread, but I have no clue on how to fix it. Disabling LogWriter the app does not throw this error.
Any help appreciated.
回答1:
I had the same issue. I don't know is it actual now, but I solved, as adviced in Official Doc. Where notice it is Garbage Collection problems:
class MyApp {
private MyLogWriter logWriter;
public void init()
{
/* Maintain reference to log writer to avoid premature cleanup by GC */
logWriter = new MyLogWriter();
epConfig.getLogConfig.setWriter(logWriter);
}
}
回答2:
As GensaGames said, you need to maintain a reference otherwise it's going to crash when the GC will decide to cleanup objects. If you use a dependency injector (eg: dagger 2), you can add you custom logwriter class to your graph dependency and set the scope to Singleton or your Application scope.
来源:https://stackoverflow.com/questions/37022757/enabling-log-in-pjsip-for-android-forces-close-the-app