[LAD] segfaulting using my ringbuffer queue, stuck!

Iain Duncan iainduncanlists at gmail.com
Fri Dec 30 19:16:08 UTC 2011


Hi folks, wondering if anyone might be able to point me at the way to sort
this out, my latest queue is segfaulting when written to or read from,
while the rest ( which are supposed to be identical except for message type
) are working great, and have been tested with full stack runs fine. I'm
banging my head on the desk at this point trying to find the difference,
but perhaps others have seen similar behaviour?

I've made a template class for a queue, that internally uses a jack
ringbuffer. I have four of them, some of my data message struct, one for a
csound note message struct, and a new one for my raw midi message struct
which looks this this:

struct MidiMessage {
   char status;
   char data_1;
   char data_2;
   int time; // time in samples when midi message arrived
};

I instantiate them before anything else and pass them into the components
that need them in their constructors

MessageQueue<DataMessage> *toEngineDataQueue = new
MessageQueue<DataMessage>();
MessageQueue<DataMessage> *fromEngineDataQueue = new
MessageQueue<DataMessage>();
MessageQueue<NoteMessage> *toEngineNoteQueue = new
MessageQueue<NoteMessage>();
MessageQueue<MidiMessage> *fromEngineMidiQueue = new
MessageQueue<MidiMessage>();

All instantiation is working fine, app starts up, and the first three
queues are working. As soon as I either write to or read from the midi
queue, I segfault. Not sure how to debug this, hints welcome! Below is the
cue code in case anyone wants to look at it. I can't see anything wrong,
but maybe I've been doing something wrong and just gotten lucky so far??

thanks
Iain

template <class Type>
class MessageQueue {

   private:
   // pointer to the ring buffer the ring buffer
   jack_ringbuffer_t *mRingBuffer;
   int mQueueLength;

   public:
    MessageQueue();
    ~MessageQueue();

   // put a msg on the queue, returns 0 or error code
   void push( Type msg );
   // store message in msg, returns true if message
   bool tryPop( Type *msg );
};

template <class Type>
MessageQueue<Type>::MessageQueue(){

   mQueueLength = DEFAULT_QUEUE_LENGTH;
   // create our ringbuffer, sized by Type
   mRingBuffer = jack_ringbuffer_create( mQueueLength * sizeof(Type) );

   // lock the buffer into memory, this is *NOT* realtime safe
   int errorLocking = jack_ringbuffer_mlock(mRingBuffer);
   if( errorLocking ){
     std::cout << "MessageQueue - Error locking memory when creating
ringbuffer\n";
   // XXX raise an exception or something?? how do we fail here??
   }

}

template <class Type>
MessageQueue<Type>::~MessageQueue(){
   cout << "MessageQueue destructor\n";
   // free the memory allocated for the ring buffer
   ack_ringbuffer_free( mRingBuffer );
}

template <class Type>
void MessageQueue<Type>::push( Type msg ){
   // write to the ring buffer, converting Type to a string
   unsigned int written = jack_ringbuffer_write( mRingBuffer, (const char
*) &msg , sizeof(Type) );

   // XXX: what to do if it fails anyway??
   if( written < sizeof(Type) ){
     cout << "Error, unable to write full message to ring buffer\n";
     // do something else here yo!
   }
}

// if a message is on the queue, get it
// returns True if it got a message
template <class Type>
bool MessageQueue<Type>::tryPop( Type *msgBuf ){

   // if there is a message on the ring buffer, copy contents into msg
   if( jack_ringbuffer_read_space( mRingBuffer) >= sizeof(Type) ){
      jack_ringbuffer_read( mRingBuffer, (char *)msgBuf, sizeof(Type) );
      // return True because a msg was read
      return 1;
   }else{
      // return False, no msg read
      return 0;
   }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linuxaudio.org/pipermail/linux-audio-dev/attachments/20111230/a0a435a9/attachment.html>


More information about the Linux-audio-dev mailing list