[LAD] segfaulting using my ringbuffer queue, stuck!

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: <linux-audio-dev@...>
Date: Friday, December 30, 2011 - 7:16 pm

--0016368e2a65adb33004b5541041
Content-Type: text/plain; charset=ISO-8859-1

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 *toEngineDataQueue = new
MessageQueue();
MessageQueue *fromEngineDataQueue = new
MessageQueue();
MessageQueue *toEngineNoteQueue = new
MessageQueue();
MessageQueue *fromEngineMidiQueue = new
MessageQueue();

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 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
MessageQueue::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
MessageQueue::~MessageQueue(){
cout << "MessageQueue destructor\n";
// free the memory allocated for the ring buffer
ack_ringbuffer_free( mRingBuffer );
}

template
void MessageQueue::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
bool MessageQueue::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;
}
}

--0016368e2a65adb33004b5541041
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

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 typ=
e ) are working great, and have been tested with full stack runs fine. I&#3=
9;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 mes=
sage struct, one for a csound note message struct, and a new one for my raw=
midi message struct which looks this this:
struct MidiMessage {=A0 =A0char status;=A0 =A0char dat=
a_1;=A0 =A0char data_2;=A0 =A0int time; // time in sampl=
es when midi message arrived};I instantiate them bef=
ore anything else and pass them into the components that need them in their=
constructors
MessageQueue<DataMessage> *toEngineDataQueue =
=3D new MessageQueue<DataMessage>(); MessageQueue<DataMessa=
ge> *fromEngineDataQueue =3D new MessageQueue<DataMessage>(); =
MessageQueue<NoteMessage> *toEngineNoteQueue =3D new MessageQueue&=
lt;NoteMessage>();
MessageQueue<MidiMessage> *fromEngineMidiQueue =3D new MessageQue=
ue<MidiMessage>();All instantiation is =
working fine, app starts up, and the first three queues are working. As soo=
n as I either write to or read from the midi queue, I segfault. Not sure ho=
w 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 doin=
g something wrong and just gotten lucky so far??
thanksIaintemplate=
<class Type>class MessageQueue {=A0=A0=A0 =A0private:<=
br>=A0 =A0// pointer to the ring buffer the ring buffer=A0 =
=A0jack_ringbuffer_t *mRingBuffer;
=A0 =A0int mQueueLength;=A0 =A0public:=A0 =A0 Mes=
sageQueue();=A0 =A0 ~MessageQueue();=A0 =A0// put a=
msg on the queue, returns 0 or error code=A0 =A0void push( Type =
msg );=A0 =A0// store message in msg, returns true if message
=A0 =A0bool tryPop( Type *msg );};template <class Type=

eueLength =3D DEFAULT_QUEUE_LENGTH;=A0 =A0// create our ringbuffer, s=
ized by Type=A0
=A0 =A0mRingBuffer =3D jack_ringbuffer_create( mQueueLength * sizeof(Type=
) );=A0 =A0// lock the buffer into memory, this is *NOT* realtime=
safe=A0 =A0int errorLocking =3D jack_ringbuffer_mlock(mRingBuffer);<=
br>=A0 =A0if( errorLocking ){
=A0 =A0 =A0std::cout << "MessageQueue - Error locking memo=
ry when creating ringbuffer\n";=A0 =A0// XXX raise an except=
ion or something?? how do we fail here??=A0 =A0}=A0 =A0}=
template <class Type>
MessageQueue<Type>::~MessageQueue(){=A0 =A0cout << "=
MessageQueue destructor\n";=A0 =A0// free the memory allocated f=
or the ring buffer=A0 =A0ack_ringbuffer_free( mRingBuffer );}=
template <class Type>
void MessageQueue<Type>::push( Type msg ){=A0 =A0// write to th=
e ring buffer, converting Type to a string=A0 =A0unsigned int written=
=3D jack_ringbuffer_write( mRingBuffer, (const char *) &msg , sizeof(T=
ype) );
=A0 =A0// XXX: what to do if it fails anyway??=A0 =A0if( wr=
itten < sizeof(Type) ){=A0 =A0 =A0cout << "Error, u=
nable to write full message to ring buffer\n";=A0 =A0 =A0// =
do something else here yo!
=A0 =A0}}// if a message is on the queue, get it// return=
s True if it got a messagetemplate <class Type>bool MessageQu=
eue<Type>::tryPop( Type *msgBuf ){=A0 =A0// if there is a m=
essage on the ring buffer, copy contents into msg
=A0 =A0if( jack_ringbuffer_read_space( mRingBuffer) >=3D sizeof(Type) =
){=A0 =A0 =A0 jack_ringbuffer_read( mRingBuffer, (char *)msgBuf, =
sizeof(Type) );=A0 =A0 =A0 // return True because a msg was read<=
br>=A0 =A0 =A0 return 1;
=A0 =A0}else{=A0 =A0 =A0 // return False, no msg read=A0 =
=A0 =A0 return 0;=A0 =A0}}

--0016368e2a65adb33004b5541041--

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[LAD] segfaulting using my ringbuffer queue, stuck!, Iain Duncan, (Fri Dec 30, 7:16 pm)