c++c++11boostmemory-managementboost-interprocess

BOOST 1.73.0 Interprocess string allocator error


I'm trying to allocate a string inside a custom object SharedValue in shared memory using Boost 1.73.0

My object:

typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> char_allocator;

class SharedValue {
public:
  SharedValue(const char_allocator& ca);
  boost::interprocess::basic_string<char, std::char_traits<char>, char_allocator> val;
};

SharedValue::SharedValue(const char_allocator& ca) : val(ca) {}

Then in the shared memory builder:

boost::interprocess::managed_shared_memory shm(boost::interprocess::open_or_create, "SharedMemory", 65536);
boost::interprocess::managed_shared_memory::allocator<char>::type ca(shm.get_allocator<char>());
auto *value = shm.find_or_construct<SharedValue>(string(ID + "_" + name + "_SharedValueSHM").c_str())(ca);

And it gives me this error:

In file included from boost/include/boost/interprocess/containers/string.hpp:23,
                 from SharedValue.h:24,
                 from SharedValue.cpp:13:
boost/include/boost/container/string.hpp: In instantiation of ‘boost::container::dtl::basic_string_base<Allocator>::members_holder::members_holder() [with Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]’:
boost/include/boost/container/string.hpp:100:18:   required from ‘boost::container::dtl::basic_string_base<Allocator>::basic_string_base() [with Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]’
boost/include/boost/container/string.hpp:643:16:   required from ‘boost::container::basic_string<CharT, Traits, Allocator>::basic_string() [with CharT = char; Traits = std::char_traits<char>; Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]’
SharedValue.cpp:19:50:   required from here
boost/include/boost/container/string.hpp:220:27: error: no matching function for call to ‘boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::allocator()’
  220 |          : allocator_type()
      |                           ^
In file included from boost/include/boost/interprocess/segment_manager.hpp:38,
                 from boost/include/boost/interprocess/detail/managed_memory_impl.hpp:30,
                 from boost/include/boost/interprocess/managed_shared_memory.hpp:25,
                 from SharedValue.h:21,
                 from SharedValue.cpp:13:
boost/include/boost/interprocess/allocators/allocator.hpp:142:4: note: candidate: ‘template<class T2> boost::interprocess::allocator<T, SegmentManager>::allocator(const boost::interprocess::allocator<T2, SegmentManager>&)’
  142 |    allocator(const allocator<T2, SegmentManager> &other)
      |    ^~~~~~~~~
boost/include/boost/interprocess/allocators/allocator.hpp:142:4: note:   template argument deduction/substitution failed:
In file included from boost/include/boost/interprocess/containers/string.hpp:23,
                 from SharedValue.h:24,
                 from SharedValue.cpp:13:
boost/include/boost/container/string.hpp:220:27: note:   candidate expects 1 argument, 0 provided
  220 |          : allocator_type()
      |                           ^
In file included from boost/include/boost/interprocess/segment_manager.hpp:38,
                 from boost/include/boost/interprocess/detail/managed_memory_impl.hpp:30,
                 from boost/include/boost/interprocess/managed_shared_memory.hpp:25,
                 from SharedValue.h:21,
                 from SharedValue.cpp:13:
boost/include/boost/interprocess/allocators/allocator.hpp:136:4: note: candidate: ‘boost::interprocess::allocator<T, SegmentManager>::allocator(const boost::interprocess::allocator<T, SegmentManager>&) [with T = char; SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>]’
  136 |    allocator(const allocator &other)
      |    ^~~~~~~~~
boost/include/boost/interprocess/allocators/allocator.hpp:136:4: note:   candidate expects 1 argument, 0 provided
boost/include/boost/interprocess/allocators/allocator.hpp:131:4: note: candidate: ‘boost::interprocess::allocator<T, SegmentManager>::allocator(boost::interprocess::allocator<T, SegmentManager>::segment_manager*) [with T = char; SegmentManager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>; boost::interprocess::allocator<T, SegmentManager>::segment_manager = boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>]’
  131 |    allocator(segment_manager *segment_mngr)
      |    ^~~~~~~~~
boost/include/boost/interprocess/allocators/allocator.hpp:131:4: note:   candidate expects 1 argument, 0 provided

Anyone have some ideas to solve this error?

Thanks


Solution

  • It compiles fine for me on 1_73_0:

    I would probably not write it like that, but slightly briefer:

    #include <boost/interprocess/containers/string.hpp>
    #include <boost/interprocess/managed_shared_memory.hpp>
    
    namespace bip = boost::interprocess;
    
    namespace Shared {
        using Mem = bip::managed_shared_memory;
        template <typename T>
            using Alloc = bip::allocator<T, Mem::segment_manager>;
    
        using String
            = bip::basic_string<char, std::char_traits<char>, Alloc<char> >;
    
        struct Value {
            Value(String::allocator_type a)
                : val(a) { }
            String val;
        };
    }
    
    int main()
    {
        using namespace Shared;
        Mem shm(bip::open_or_create, "SharedMemory", 65536);
    
        auto* value = shm.find_or_construct<Value>(
            "ID_name_SharedValueSHM")(shm.get_segment_manager());
    }
    

    This would sidestep the apparent type mismatch between youd char_allocator and the result of boost::interprocess::managed_shared_memory::allocator<char>::type . Really, you should probably check that it is using the correctly spelled typedefs in your real code.

    Leveraging the implicit construction of the allocator from a segment manager pointer. If you can, accept heterogeneous allocators, because they can interconvert:

    template <typename Alloc>
    explicit Value(Alloc a) : val(a) { }
    

    This will help when you get more advanced and use e.g. scoped allocators.