July 24, 2011

tkrajacic tkrajacic
Lab Rat
3 posts

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:

  1. int init_shared_memory() {
  2.  
  3.       int size = sizeof(float);
  4.  
  5. // after the first run of this method this always outputs that we are attached and we always have the same key
  6. // this is how it is supposed to be
  7. if(FGEDLinkMemory.isAttached()) {
  8. // we are still attached
  9. cout << "-----------------------------> attached!" << endl;
  10. cout << "key of attached: " << FGEDLinkMemory.key().toStdString() << endl;
  11. }
  12.  
  13. // this should not return true both times!? but it does! and that is why the following actual method fails
  14. int result = (int)FGEDLinkMemory.create(size);
  15. cout << result << endl;
  16. result = (int)FGEDLinkMemory.create(size);
  17. cout << result << endl;
  18.  
  19. // this works perfectly on Mac OS X but fails on windows because the if statement is always false (because of the "!" off)
  20.       if (!FGEDLinkMemory.create(size)) {
  21.     cout << "weird" << endl;
  22.          if(FGEDLinkMemory.error() == QSharedMemory::AlreadyExists) {
  23.             // our shared memory already exists so we can continue
  24.             cout << "=> init: Shared memory exists!" << endl;
  25.             if(FGEDLinkMemory.isAttached()) {
  26.                // we are attached
  27.                cout << "attached!" << endl;
  28.             } else {
  29.                cout << "NOT attached!" << endl;
  30.                FGEDLinkMemory.attach();
  31.             }
  32.          } else {
  33.             cout << "=> init: Creating shared memory failed!" << endl;
  34.             cout << FGEDLinkMemory.errorString().toStdString() << endl;
  35.             return 0;
  36.          }
  37.       } else {
  38.          cout << "=> init: Size of Memory assigned: "<< size << " bytes" << endl;
  39.          cout << "=> init: Key of Memory: "<< FGEDLinkMemory.key().toStdString() << endl;
  40.       }
  41.       return 1;
  42.      
  43.    }

3 replies

July 27, 2011

tkrajacic tkrajacic
Lab Rat
3 posts

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?)?

July 27, 2011

mlong mlong
Robot Herder
1537 posts

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.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

July 28, 2011

tkrajacic tkrajacic
Lab Rat
3 posts

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:

  1. 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!

 
  ‹‹ Qt scanner generator      [Solved]Qt Primary Expression error? ››

You must log in to post a reply. Not a member yet? Register here!