2013-03-19 148 views
0

我正在使用QSharedMemory存儲一些數據,並希望隨後將數據附加到那裏。所以我用新數據多次調用下面的代碼。 「audioBuffer」是賦予此功能的新數據。在memcpy操作出現seg錯誤之前,我可以調用這個函數大約4-7次(並且它會變化)。 QSharedMemory位置的大小非常大,因此在seg錯誤之前進行的少量調用中,不存在memcpy複製超出其邊界的數據的問題。另外,m_SharedAudioBuffer.errorString()在memcpy操作之前不會發生錯誤。目前,我只有一個使用此QSharedMemory段的進程。我也試着不斷追加編寫,並且工作正常,所以當我嘗試向共享內存段添加更多數據時發生了一些情況。有任何想法嗎?謝謝!Qt QSharedMemory幾次成功寫入後的分段錯誤

// 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(); 

編輯:也許,這是由於我的目標嵌入式設備是非常小的。當我嘗試寫入共享內存時,可用的RAM很大,但是我注意到在/ tmp目錄中(只給出了4Mb),我有以下條目 - 雖然在/ tmp中大小几乎不會消耗,不能確定爲什麼我不能分配更多的內存,還QSharedMemory :: create方法永遠不會失敗對我的960000最大尺寸:

# cd /tmp/ 
# ls 
QtSettings 
lib 
qipc_sharedmemory_AudioBufferData2a7d5f1a29e3d27dac65b4f350d76a0dfd442222 
qipc_sharedmemory_AudioBufferSizeData6b7acc119f94322a6794cbca37ed63df07b733ab 
qipc_systemsem_AudioBufferData2a7d5f1a29e3d27dac65b4f350d76a0dfd442222 
qipc_systemsem_AudioBufferSizeData6b7acc119f94322a6794cbca37ed63df07b733ab 
qtembedded-0 
run 

回答

0

的問題似乎是,我使用的QByteArray的:: fromRawData上指針由共享內存段返回。當我在該指針上使用memcpy顯式複製數據,然後使用複製的數據構造我的QByteArray時,seg故障停止。