Pregunta

I'm using QSharedMemory to store some data and want to subsequently append data to what is contained there. So I call the following code several times with new data. The "audioBuffer" is new data given to this function. I can call this function about 4-7 times ( and it varies ) before it seg faults on the memcpy operation. The size of the QSharedMemory location is huge so in the few calls that I do before seg faulting, there is no issue of memcpy copying data beyond it's boundaries. Also, m_SharedAudioBuffer.errorString() gives no errors up to the memcpy operation. Currently, I only have one process using this QSharedMemory segment. I also tried to write continually without appending each time and that works fine, so something is happening when I try to append more data to the shared memory segment. Any ideas? Thanks!

    // Get the buffer size for the current audio buffer in shared memory
    int bufferAudioDataSizeBytes = readFromSharedAudioBufferSizeMemory(); // This in number of bytes

    // Create a bytearray with our data currently in the shared buffer
    char* bufferAudioData = readFromSharedAudioBufferMemory();
    QByteArray currentAudioStream = QByteArray::fromRawData(bufferAudioData,bufferAudioDataSizeBytes);
    QByteArray currentAudioStreamDeepCopy(currentAudioStream);

    currentAudioStreamDeepCopy.append(audioBuffer);
    dataSize = currentAudioStreamDeepCopy.size();

    //#if DEBUG
    qDebug() << "Inserting audio buffer, new size is: " << dataSize;
    //#endif

    writeToSharedAudioBufferSizeMemory( dataSize ); // Just the size of what we received

    // Write into the shared memory
    m_SharedAudioBuffer.lock();

    // Clear the buffer and define the copy locations
    memset(m_SharedAudioBuffer.data(), '\0', m_SharedAudioBuffer.size());
    char *to = (char*)m_SharedAudioBuffer.data();
    char *from = (char*)audioBuffer.data();

    // Now perform the actual copy operation to store the buffer
    memcpy( to, from, dataSize );

    // Release the lock
    m_SharedAudioBuffer.unlock();

EDIT: Perhaps, this is due to my target embedded device which is very small. The available RAM is large when I am trying to write to shared memory, but I notice that in the /tmp directory ( which is only given 4Mb ) I have the following entries - the size is not nearly consumed in /tmp though so I'm not sure why I couldn't allocate more memory, also the QSharedMemory::create method never fails for my maximum size of 960000:

# cd /tmp/
# ls
QtSettings
lib
qipc_sharedmemory_AudioBufferData2a7d5f1a29e3d27dac65b4f350d76a0dfd442222
qipc_sharedmemory_AudioBufferSizeData6b7acc119f94322a6794cbca37ed63df07b733ab
qipc_systemsem_AudioBufferData2a7d5f1a29e3d27dac65b4f350d76a0dfd442222
qipc_systemsem_AudioBufferSizeData6b7acc119f94322a6794cbca37ed63df07b733ab
qtembedded-0
run
¿Fue útil?

Solución

The problem seemed to be that I was using QByteArray's ::fromRawData on the pointer returned by the shared memory segment. When I copied that data explicitly using memcpy on this pointer, and then constructed my QByteArray using the copied data, then the seg faults stopped.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top