Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7686363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:27:45+00:00 2026-05-31T19:27:45+00:00

I’m developing an Android 3.1 application that uses USB Host mode to communicate with

  • 0

I’m developing an Android 3.1 application that uses USB Host mode to communicate with my keyboard (Korg M3) via MIDI over USB. This is running on my Xoom with Android 4.0.3 installed. I’m able to receive MIDI messages over USB without any problems, but sending note data back to my keyboard is having mixed success, with frequent crashes after a half-second delay.

Here’s the error I keep getting as I tap the button on the Action Bar to send a note:

E/dalvikvm(6422): JNI ERROR (app bug): accessed stale global reference 0x1da0020a (index 130 in a table of size 130)

What I’ve checked/tried to track down the cause:

  • Since the code is multithreaded, I have Java synchronized blocks surrounding accesses to the output request pool, input request pool (as per the ADB sample in the Android documentation), and a custom lock object for the current output request & associated ByteBuffer object references. I have structured the code that performs these locks to minimise the likelihood of deadlocks occurring.
  • When retrieving an available UsbRequest object from the relevant request pool, I am setting the clientData reference to a new ByteBuffer object, rather than reusing the previously-associated ByteBuffer object and calling clear() on it.
  • I have added extensive logging calls (for logCat) at the critical points in the code to try and track down where exactly it’s failing. What I’ve found is that the error eventually occurs at the following point (this code works fine for a few times up until then):

    public void sendMidiData()
    {
        synchronized(_outputLock)
        {
            if(_currentOutputRequest == null || _outputBuffer.position() < 2)
                return;
    
            Log.d(_tag, "Queuing Send request");
            //// ERROR - happens in this next statement:
            _currentOutputRequest.queue(_outputBuffer, _maxPacketSize);
            Log.d(_tag, "Send request queued; resetting references...");
            //Initialise for next packet
            _currentOutputRequest = null; //getAvailableSendRequest();
            _outputBuffer = null; //(ByteBuffer)_currentOutputRequest.getClientData();
            Log.d(_tag, "Output request & buffer references set.");
        }
    }
    
  • I have also tried setting the _currentOutputRequest and _outputBuffer references to null so that the available request is retrieved only when the next MIDI event is to be written to the buffer. If null is replaced by the original calls (as shown commented out), the next available request is retrieved immediately. This has made no difference.

Is there any idea as to what would be causing this issue? Could this be a previously-undiscovered bug in Android? A search on the error returned doesn’t bring back much; mainly references to NDK programming (which I’m not doing).

Cheers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:27:47+00:00Added an answer on May 31, 2026 at 7:27 pm

    Okay, I had a bit of a breakthrough and found two issues:

    • The call to _currentOutputRequest.queue(_outputBuffer, _maxPacketSize); was passing the entire buffer capacity _maxPacketSize, which has a constant value of 64 (bytes). Apparently this only works for bulk reads, where up to 64 bytes will be read; bulk Send requests need to specify the exact number of bytes being sent.
    • The _currentOutputRequest.queue() and _connection.requestWait() method calls appear not to be thread-safe, particularly in the implementation of UsbDeviceConnection (which is the type of _connection). I suspect the UsbRequest _currentOutputRequest internally uses the UsbConnection object when queuing a Send request.

    How I fixed this:

    The call to queue() is changed to:

    _currentOutputRequest.queue(_outputBuffer, _outputBuffer.position());
    

    For the second issue, the queue() statement already occurs inside a synchronized block, using the _outputLock object. In the Run() method of the reader thread, I have had to wrap the call to requestWait() in a synchronized block also using outputLock:

    UsbRequest request = null;
    synchronized(_outputLock)
    {
        // requestWait() and request.queue() appear not to be thread-safe.
        request = _connection.requestWait();
    }
    

    Given that this occurs in a while loop and requestWait blocks the thread, one major issue I found is that the lock causes starvation when trying to queue a Send request. The result is that while the application receives and acts on incoming MIDI data in a timely fashion, outputted MIDI events are significantly delayed.

    As a partial fix for this, I inserted a yield statement just before the end of the while loop to keep the UI thread unblocked. (The UI thread is temporarily being used as a note event is triggered by a button press; this will eventually use a separate playback thread.) As a result it’s better, but not perfect, since there’s still quite a delay before the first output note is sent.

    A better solution:

    To resolve the demand on a mutual lock for reading and writing asynchronously, the asynchronous queue() and requestWait() methods are only used for read operations, which remain on the separate ‘reader’ thread. Because of this, the synchronized block is unnecessary, so this segment can be reduced to the following:

    UsbRequest request = _connection.requestWait();
    

    As for write/send operations, the core of this is moved to a separate thread for executing synchronous bulkTransfer() statements:

    private class MidiSender extends Thread
    {
        private boolean _raiseStop = false;
        private Object _sendLock = new Object();
    
        private LinkedList<ByteBuffer> _outputQueue = new LinkedList<ByteBuffer>();
    
        public void queue(ByteBuffer buffer)
        {
            synchronized(_sendLock)
            {
                _outputQueue.add(buffer);
                // Thread will most likely be paused (to save CPU); need to wake it
                _sendLock.notify();
            }
        }
    
        public void raiseStop()
        {
            synchronized (this)
            {
                _raiseStop = true;
            }
    
            //Thread may be blocked waiting for a send
            synchronized(_sendLock)
            {
                _sendLock.notify();
            }
        }
    
        public void run()
        {
            while (true)
            {
                synchronized (this)
                {
                    if (_raiseStop) 
                        return;
                }
    
                ByteBuffer currentBuffer = null;
                synchronized(_sendLock)
                {
                    if(!_outputQueue.isEmpty())
                        currentBuffer =_outputQueue.removeFirst(); 
                }
    
                while(currentBuffer != null)
                {
                    // Here's the synchronous equivalent (timeout is a reasonable 0.1s):
                    int transferred = _connection.bulkTransfer(_outPort, currentBuffer.array(), currentBuffer.position(), 100);
    
                    if(transferred < 0)
                        Log.w(_tag, "Failed to send MIDI packet");
    
                    //Process any remaining packets on the queue
                    synchronized(_sendLock)
                    {
                        if(!_outputQueue.isEmpty())
                            currentBuffer =_outputQueue.removeFirst();
                        else
                            currentBuffer = null;
                    }
                }
    
                synchronized(_sendLock)
                {
                    try
                    {
                        //Sleep; save unnecessary processing
                        _sendLock.wait();
                    }
                    catch(InterruptedException e)
                    {
                        //Don't care about being interrupted
                    }
                }
    
            }           
        }
    }
    

    At first I was concerned about the asynchronous code conflicting with the above (since they share the same UsbDeviceConnection), but this seems not to be an issue since they’re using completely different UsbEndpoint instances.

    The good news is that the application runs more smoothly and doesn’t crash when sending notes at the same time as I’m playing the keyboard.

    So in general (for bi-directional USB communication on separate endpoints), it seems that the asynchronous methods are best suited for read/input operations where we don’t need to worry about defining polling behaviour (whether or not this is what happens internally), while output/send operations are better served by the synchronous bulkTransfer() method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.