c++ - Qt QSharedMemory 如何更新共享内存位置

标签 c++ qt shared-libraries shared-memory qtembedded

我有一个库( teSTLib-lib.so ),它由 2 个不同的 Qt 应用程序链接。我的问题是初始化后无法更新共享内存。

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 在其 main() 中具有以下内容:

// 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共享内存”后,我似乎无法更新那里的文本存储。当我在应用程序 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;

为什么我似乎无法更新共享内存?我究竟做错了什么?谢谢-

最佳答案

我在这里打印出“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();

}

没有关联的 key ,因此它没有分离。所以我把这个函数改成了:

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

}

它有效 - 我可以很好地修改共享内存。

关于c++ - Qt QSharedMemory 如何更新共享内存位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14735134/

相关文章:

c++ - 如何获取 Mac OS X Dock 的位置、宽度和高度? cocoa /碳/C++/Qt

c++ - Linux:如何测量进程内线程的内存使用情况?

python - 将 unique_ptr 与 boost python 结合使用 - boost::shared_ptr 有效,但 unique_ptr 无效

c++ - 是否可以更改轴在 qwt 中的位置?

c++ - Qt - 检测 QListView 中的项目信息变化

c++ - 如何在 Qt Creator 中启用 C++11?

c++ - Qt creator,错误信息

c++ - 将模板实例导出为 C 函数

linux - 加载现有库失败

c++ - 什么库包含 map STL 集合?