0

boost::interprocess::shared_memory_object can be constructed with the default constructor, which creates an empty shared_memory_object (i.e. one that has no shared memory owned by it). Depending on the situation, my code might construct a shared_memory_object that is empty. This means that sometimes I would need to check if a given shared_memory_object is empty.

While this seems like something that's really easy to do, nowhere in the documentation does describe how to test a shared_memory_object for emptiness!

I've looked at the code, which suggests that shared_memory_object::get_name() returns an empty C-string when there is no associated shared memory, but nowhere does it seem to guarantee that I will never get the empty C-string when shared memory is associated. Furthermore, shared_memory_object::get_size() might return false even if there is associated shared memory, if the size cannot be obtained, so this method can't be reliably used either.

What is the proper way to test a shared_memory_object for emptiness?

Bernard
  • 5,209
  • 1
  • 34
  • 64
  • You could try passing empty strings to the other constructors and then see what you get in `get_name()` : if you get a non-empty string, it means it will be empty only for the default constructor and you're good to go. – Olivier Sohn Jul 24 '18 at 06:12
  • @OlivierSohn That feels dangerous. Also, the implementations for are different for POSIX systems as compared to Windows and other systems, so what works on one system might not work on another... – Bernard Jul 24 '18 at 06:22
  • that's a first step, then you can check all implementations and verify that the guarantees hold in all cases... – Olivier Sohn Jul 24 '18 at 06:23

0 Answers0