QSharedMemory create() on Windows always returns true even if already exists?



  • The create method is supposed to return false if a shared memory segment with the same name already exists i thought?
    It works on Mac OS X but on windows the same code returns always true even though the process where I want to call create is still attached to said memory segment.

    I have this code that should create the memory segment the first time it is called and subsequently only attach to the existing memory.

    Can anyone shed some light on why this is happening?

    Here is a stripped down version of my code:

    @int init_shared_memory() {

      int size = sizeof(float);
    

    // after the first run of this method this always outputs that we are attached and we always have the same key
    // this is how it is supposed to be
    if(FGEDLinkMemory.isAttached()) {
    // we are still attached
    cout << "-----------------------------> attached!" << endl;
    cout << "key of attached: " << FGEDLinkMemory.key().toStdString() << endl;
    }

    // this should not return true both times!? but it does! and that is why the following actual method fails
    int result = (int)FGEDLinkMemory.create(size);
    cout << result << endl;
    result = (int)FGEDLinkMemory.create(size);
    cout << result << endl;

    // this works perfectly on Mac OS X but fails on windows because the if statement is always false (because of the "!" off)
    if (!FGEDLinkMemory.create(size)) {
    cout << "weird" << endl;
    if(FGEDLinkMemory.error() == QSharedMemory::AlreadyExists) {
    // our shared memory already exists so we can continue
    cout << "=> init: Shared memory exists!" << endl;
    if(FGEDLinkMemory.isAttached()) {
    // we are attached
    cout << "attached!" << endl;
    } else {
    cout << "NOT attached!" << endl;
    FGEDLinkMemory.attach();
    }
    } else {
    cout << "=> init: Creating shared memory failed!" << endl;
    cout << FGEDLinkMemory.errorString().toStdString() << endl;
    return 0;
    }
    } else {
    cout << "=> init: Size of Memory assigned: "<< size << " bytes" << endl;
    cout << "=> init: Key of Memory: "<< FGEDLinkMemory.key().toStdString() << endl;
    }
    return 1;

    }@



  • Mhmmm, no one?
    I consider this to be a severe bug actually...
    Or am I just stupidly misinterpreting the Documentation (read: I am an idiot?)?


  • Moderators

    Interesting... just to eliminate the obvious, the Key being assigned to FGEDLinkMemory is consistent among instances, right? Don't see the code that's setting it, so wasn't sure.



  • That's how it is set in the constructor, and when printing the key, that's what also comes out for all calls to initialize:
    @QSharedMemory FGEDLinkMemory("FGEDLinkMemory");@

    I call this init function in a dynamically linked shared library and I have to make sure that my shared memory is set up, so all other functions of the library first call the init method so they are surely attached to the memory.

    I now have a bool is_initialized that get's set and immediately exits the init function if it was already called once, but i think it shouldn't be necessary.(And every process runs through it anyway once since this bool is not shared)

    Maybe I am making just a dumb design mistake by doing things that way?

    I thought it was the easiest way to make sure all programs calling my DLL can just call functions and don't need to call some init_function first. It's easier for the programmer who uses my DLL I thought.

    Thanks for taking interest btw!


Log in to reply
 

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