The specified message queue synchronization barrier token has not been posted

13,161

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.

Share:
13,161
serenskye
Author by

serenskye

Updated on June 18, 2022

Comments

  • serenskye
    serenskye almost 2 years

    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 :)

  • Nikolai
    Nikolai almost 7 years
    It might be a bit late to ask, but why do we need to do this?
  • Artem Mostyaev
    Artem Mostyaev almost 7 years
    Because Android use recycled objects pool for Messages instances. All instances, associatiated with Message.obtain can link to other intance since some time. So you have to re-create queued instances to keep them.