QSharedMemory with to sessions



  • Hello,

    I'm just working on an mobile application. This application consists of two layer (can and gui layer). Every layer is a own application. Between this two applications I'm sharing data with QSharedMemory. So when data is coming in from the CAN-Bus to the CAN layer, I send these with the shared memory to the Gui layer. That works fine!

    Now I would like to send other data from the GUI layer to the CAN layer. So in the other direction. I do this with another class but with the same functions with another key. Write data in the gui layer seems to work. But read the data in the can layer is not possible. The message is: can't attach "QSharedMemory::handle: UNIX key file doesn't exist"

    Here the code:

    Writing in the shared memory on the gui layer:
    @void DeviceAdrHandlingOUT::writeToSharedMemory(QString str)
    {
    QSharedMemory sharedMem("DeviceOUT");
    // sharedMemory.setKey("Deviceout");
    if (sharedMem.isAttached())
    {
    sharedMem.detach();
    }

    QBuffer buffer;
    buffer.open(QBuffer::ReadWrite);
    QDataStream out(&buffer);
    
    out << str;
    
    int size = buffer.size();
    
    if (!sharedMem.create(DEVICES_SHARED_MEMORY_BUFFER_SIZE))
    {
        qDebug() << "Unable to create shared memory segment.";
    

    // return;
    }

    qDebug() << "err" << sharedMem.errorString();
    sharedMem.lock();
    char *to = (char*)sharedMem.data();
    const char *from = buffer.data().data();
    memcpy(to, from, qMin(sharedMem.size(),size));
    sharedMem.unlock();
    

    }@

    and here I read in the can layer:

    @QString DeviceAdrHandlingOUT::read_shared_Memory()
    {
    sharedMemory.setKey("DeviceOUT");
    if(!sharedMemory.attach())
    {
    qDebug() << "can't attach" << sharedMemory.errorString();
    }

    QBuffer buffer;
    QDataStream in(&buffer);
    QString str;
    
    sharedMemory.lock();
    buffer.setData((char*)sharedMemory.constData(), sharedMemory.size());
    buffer.open(QBuffer::ReadOnly);
    
    in >> str;
    
    sharedMemory.unlock();
    sharedMemory.detach();
    
    return str;
    

    }@

    So when I'm using the shared memory from the can to the gui layer --> it works!
    When I'm using the shared memory from the gui to the can layer --> it doesn't works!
    I'm using another key!

    Do anyone has an idea?

    Thanks,

    Bobby



  • I think that the function will be the following:

    @QString DeviceAdrHandlingOUT::read_shared_Memory()
    {
    QSharedMemory sharedMemory("DeviceOUT"); // <== MY CHANGE

    if(!sharedMemory.attach())
    {
        qDebug() << "can't attach" << sharedMemory.errorString();
    }
    
    QBuffer buffer;
    QDataStream in(&buffer);
    QString str;
    
    sharedMemory.lock();
    buffer.setData((char*)sharedMemory.constData(), sharedMemory.size());
    buffer.open(QBuffer::ReadOnly);
    
    in >> str;
    
    sharedMemory.unlock();
    sharedMemory.detach();
    
    return str;
    

    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.