libc: ../src/pj/os_core_unix.c:692: pj_thread_this: assertion "!"Calling pjlib from unknown/external

来源:互联网 发布:长沙麻将源码 编辑:程序博客网 时间:2024/06/05 11:59

使用PjSip通信的时候,在切换用户的时候经常会报这个错,走过很多弯路,最后才发现是Java GC回收导致对象被销毁:

http://www.pjsip.org/docs/book-latest/html/intro_pjsua2.html#problems-with-garbage-collection

Garbage collection (GC) exists in Java and Python (and other languages, but we don’t support those for now), and there are some problems with it when it comes to PJSUA2 usage:

1.premature destruction of PJSUA2 objects which are created in Java and Python space and passed to the native space without keeping reference to the object

2.it delays the destruction of objects (including PJSUA2 objects), causing the code in object’s destructor to be executed out of order

3.the destruction operation by GC may run on different thread not previously registered to PJLIB, causing assertion


我们原先的消息发送是先添加好友,然后发送消息的时候直接选择好友,如下:

初始化好友:

public static void initBuddy(final String buddyUri, final String userId) {    new Handler(Looper.getMainLooper()).post(new Runnable() {        @Override        public void run() {            Log.e("will", "buddyUrl" + buddyUri);            BuddyConfig bcfg = new BuddyConfig();            bcfg.setUri(buddyUri);            try {                buddy = account.addBuddy(bcfg);                LogUtil.println(TAG + "initBuddy ", "buddy = " + buddy);                addBuddyList(userId, buddy);                Constants.userIdList.add(userId);                buddy.subscribePresence(true);            } catch (Exception e) {                e.printStackTrace();            }        }    });}
发送消息:

public void pjsipSendMsg(final String content,  String userId) {    SendInstantMessageParam prm = new SendInstantMessageParam();    prm.setContent(content);    try {        //    myBuddy.create(account, bCfg);        myBuddy.sendInstantMessage(prm);    } catch (Exception e) {        e.printStackTrace();        return;    }}

后来我们发现发送消息的时候这样经常会报错

libc: ../src/pj/os_core_unix.c:692: pj_thread_this: assertion "!"Calling pjlib from unknown/external

所以我们根据文档改成了这样:

public void pjsipSendMsg(final String content,  String userId) {    if (userId == null || userId.equals("")){        userId = Constants.userId;    }    String buddy_uri = "<sip:" + userId + "@" + Constants.sipDomain + ";transport=tcp" + ">";    LogUtil.println(TAG + "pjsipSendMsg"," buddy_uri = " + buddy_uri);    BuddyConfig bCfg = new BuddyConfig();    bCfg.setUri(buddy_uri);    bCfg.setSubscribe(false);    MyBuddy myBuddy = account.addBuddy(bCfg);    SendInstantMessageParam prm = new SendInstantMessageParam();    prm.setContent(content);    try {        //    myBuddy.create(account, bCfg);        myBuddy.sendInstantMessage(prm);        myBuddy.delete();    } catch (Exception e) {        e.printStackTrace();        return;    }}
直接将初始化好友和发送消息集成到一个方法里,发送完消息后立刻将好友销毁,下次再发送消息的时候就重新初始化。换用这种方式后就再也没有遇到过之前的问题!希望能帮到你们!

0 0
原创粉丝点击