Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The specified message queue synchronization barrier token has not been posted

I have an app which as a binded service. I am sending messages to the service, but sometimes I am getting the following error:

E/AndroidRuntime(28216): java.lang.IllegalStateException: The specified message queue synchronization barrier token has not been posted or has already been removed.

Sometimes I get this error instead:

android.util.AndroidRuntimeException: { what=888 when=0 } This message is already in use.

Sometimes the UI just freezes. I am communicating from the service to the activity and visa versa through handlers.

 public void init(IBinder service){
    playerService = new Messenger(service);
    setBound(true);
    try {
        Message msg = Message.obtain(null, PlayerService.MSG_REGISTER_CLIENT);
        msg.replyTo = messenger;
        playerService.send(msg);
        while(!messageQueue.isEmpty()){
            playerService.send(messageQueue.remove());
        }
    } catch (RemoteException e) {
        // In this case the service has crashed before we could even do anything with it
        Log.d(Player.TAG, "problem binding player messenger " + e.getMessage());
    }
}

Here is a method which consistenly results on freezes, the second time it is called.

public void play(String url) {
    Message msg = Message.obtain(null, PlayerService.PLAY, 0, 0);
    msg.setData(getURLBundle(url));
    sendMessage(msg);
}

private void sendMessage(Message message){
    if(!isBound){
        Log.d(Player.TAG, "isnt bound, queueing message");
        messageQueue.add(message);
    }else {
        try {
            playerService.send(message);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

I'm new to Threading, Messengers and Handlers, so any help is appreciated, thanks :)

like image 683
serenskye Avatar asked May 14 '13 18:05

serenskye


1 Answers

Problem is that you queue messages, that are allocated by message.obtain(). You need to create copy of message by calling

Message m = new Message();
m.copyFrom(message);

and only then add copied message m into the queue.

like image 87
Artem Mostyaev Avatar answered Oct 21 '22 22:10

Artem Mostyaev