2013-02-06 47 views
2

我有一個庫(testlib-lib.so),它由2個不同的Qt應用程序鏈接。我的問題是我無法在初始化後更新共享內存。Qt QSharedMemory如何更新共享內存位置

void TestLib::createInitialSharedMemory() { 

    // Create shared memory 
    qDebug() << "Creating new shared memory..."; 
    sharedAudioMemory.setKey("globalAudioBufferSharedMemory"); 

    // First, test whether a shared memory segment is already attached to the process. 
    // If so, detach it 
    if (sharedAudioMemory.isAttached()) 
    { 
     sharedAudioMemory.detach(); 
    } 

    // Try to create memory of our required size 
    int sizeOfSharedData = 60 * 8000 * 2; // 60 Seconds x 8 KHz * 2 Bytes (signed short) 
    if (!sharedAudioMemory.create(sizeOfSharedData)) 
    { 
     qDebug() << "ERROR: Failed to Allocate Shared Memory of size: " << sizeOfSharedData; 
    } 

} 

void TestLib::writeToSharedMemory(QString text) { 

    // Create a buffer and data stream and some text to shove in there 
    QBuffer buffer; 
    buffer.open(QBuffer::ReadWrite); 
    QDataStream out(&buffer); 
    out << text; 
    int size = buffer.size(); 

    if (!sharedAudioMemory.attach()) { 

     // If an attempt of reading from the shared memory before data is written 
     qDebug() << "Cannot attach to shared memory to update!"; 

    } 

    // Write into the shared memory 
    sharedAudioMemory.lock(); 
    qDebug() << "Writing data to buffer: " << text; 
    char *to = (char*)sharedAudioMemory.data(); 
    const char *from = buffer.data().data(); 
    memcpy(to, from, qMin(sharedAudioMemory.size(), size)); 
    sharedAudioMemory.unlock(); 

} 

Qt的應用程序A具有以下在它的主要():

// Create the shared memory and load some data 
TestLib loadData; 
loadData.createInitialSharedMemory(); 
loadData.writeToSharedMemory("12345 init shared memory"); 

// This is the key 
QSharedMemory sharedAudioMemory("globalAudioBufferSharedMemory"); 

// Attempt to attach to shared memory for the audio buffer 
if (!sharedAudioMemory.attach()) { 

    //If an attempt of reading from the shared memory before data is written 
    qDebug() << "ERROR: Failed to attach to shared memory..."; 
    return -1; 

} 

// Define a buffer to read the data in 
QBuffer buffer; 
QDataStream in(&buffer); 
QString text; 

// Lock the memory and read the data 
sharedAudioMemory.lock(); 
char* pointer = (char*)sharedAudioMemory.constData(); 
buffer.setData((char*)sharedAudioMemory.constData(), sharedAudioMemory.size()); // Sets the contents of the internal buffer to be the first size bytes of data. 
buffer.open(QBuffer::ReadOnly); 
in >> text; 
sharedAudioMemory.unlock(); 
sharedAudioMemory.detach(); 

Qt的應用程序B,試圖通過執行更新共享存儲器:

void Control::updateText() { 

    // Create seed for the random 
    // That is needed only once on application startup 
    QTime time = QTime::currentTime(); 
    qsrand((uint)time.msec()); 

    // Get random value between 0-100 
    int randomValue = qrand() % ((1000 + 1) - 2) + 2; 

    // Create the shared memory and load some data 
    TestLib loadData; 
    QString randomText = QString("%1 test asdf %2 test asdf %3 test asdf %4").arg(randomValue).arg(randomValue).arg(randomValue).arg(randomValue); 
    loadData.writeToSharedMemory(randomText); 

} 

初始化共享後內存到「12345 init共享內存」,我似乎無法更新那裏的文本存儲。當我調用App B中的updateText()方法時,庫方法writeToSharedMemory()被調用,並且方法中的qDebug()打印我希望在共享內存中更新的文本,但是當我試圖更新共享內存,它始終打印原始文本「12345 init共享內存」:

// Attempt to attach to shared memory for the audio buffer 
if (!sharedAudioMemory.attach()) { 

    // If an attempt of reading from the shared memory before data is written 
    qDebug() << "ERROR: Failed to attach to shared memory..."; 

} 

// Define a buffer to read the data in 
QBuffer buffer; 
QDataStream in(&buffer); 
QString text; 

// Lock the memory and read the data 
sharedAudioMemory.lock(); 
char* pointer = (char*)sharedAudioMemory.constData(); 
buffer.setData((char*)sharedAudioMemory.constData(), sharedAudioMemory.size()); // Sets the contents of the internal buffer to be the first size bytes of data. 
buffer.open(QBuffer::ReadOnly); 
in >> text; 
sharedAudioMemory.unlock(); 
sharedAudioMemory.detach(); 

// Print the data 
qDebug() << "SHARED TEXT IS: " << text; 

爲什麼我可以更新共享內存?我究竟做錯了什麼?謝謝 -

+0

我不知道這是否是問題的原因,但是在我看來,所有對detach()的調用都是一個壞主意,因爲根據文檔,當你的兩個進程已經分離時,'它由系統釋放,即內容被銷燬「。 –

回答

3

我發現錯誤打印出後「Errorstring,則()」在這裏:

if (!sharedAudioMemory.attach()) { 

     // If an attempt of reading from the shared memory before data is written 
     qDebug() << "Cannot attach to shared memory to update! ERROR: " << sharedAudioMemory.errorString(); 

} 

有沒有關聯,因此不分離的關鍵。所以我改變了這個功能:

// Assign the key so we can update the data 
sharedAudioMemory.setKey("globalAudioBufferSharedMemory"); 
if (!sharedAudioMemory.attach()) { 

    // If an attempt of reading from the shared memory before data is written 
    qDebug() << "Cannot attach to shared memory to update! ERROR: " << sharedAudioMemory.errorString(); 

} 

它的工作原理 - 我可以修改共享內存就好了。