Search code examples
c++boostmemory-mapped-filesinterprocesscircular-buffer

circular_buffer and managed_mapped_file segmentation fault


I am using boost 1.73.0, and am trying to use circular_buffer together with manage_mapped_file to store strings in a circular buffer persisted on disk.

I do the following to create/open the circular_buffer:

boost::interprocess::managed_mapped_file mmf(boost::interprocess::open_or_create, "./circ_buffer.bin", 10u << 10);
typedef boost::interprocess::allocator<std::string, boost::interprocess::managed_mapped_file::segment_manager> string_allocator;
typedef boost::circular_buffer<std::string, string_allocator> circ_buf;
circ_buf* instance = mmf.find_or_construct<circ_buf>("some_name")(10, mmf.get_segment_manager());

This works well and I can put strings in the circular_buffer like this:

for(int idx = 0 ; idx < 15; idx++) {
    std::string v = "mystring1-" + std::to_string(idx);
    instance->push_back(v);
}

Looking at the raw file (even though it is binary) I dosee the strings inthere, so it seems like the circular_buffer was indeed persisted.

But, if I try to load the circular_buffer in another process as shown in the first code snippet and read the first element like this:

instance->front()

I get a segmentation fault. I know that in the end I will need sychronization around memory access, but this should not be the problem in the example above as only one process is accessing the file a any given time.

The funny thing is that if I substitute std::string with char in the allocator I do not get the segmentation fault. What am I doing wrong?

Rgds Klaus


Solution

  • boost::interprocess::managed_mapped_file mmf(boost::interprocess::open_or_create, "./circ_buffer.bin", 10u << 10);
    typedef boost::interprocess::allocator<std::string, boost::interprocess::managed_mapped_file::segment_manager> string_allocator;
    typedef boost::circular_buffer<std::string, string_allocator> circ_buf;
    circ_buf* instance = mmf.find_or_construct<circ_buf>("some_name")(10, mmf.get_segment_manager());
    

    You allocate the string objects in shared memory. However, string itself allocates, and you never told it to use the shared-memory allocator.

    The reason you may be seeing short strings inside the data is because of SSO

    Fixed Version

    Live On Coliru

    #include <boost/circular_buffer.hpp>
    #include <boost/interprocess/managed_mapped_file.hpp>
    #include <boost/interprocess/containers/string.hpp>
    #include <iostream>
    #include <iomanip>
    namespace bip = boost::interprocess;
    
    namespace Shared {
        using Mem = bip::managed_mapped_file;
        using Segment = Mem::segment_manager;
    
        template <typename T> using Alloc = bip::allocator<T, Segment>;
        template <typename T> using Buffer = boost::circular_buffer<T, Alloc<T> >;
    
        using String = bip::basic_string<char, std::char_traits<char>, Alloc<char> >;
        using StringBuf = Buffer<String>;
    }
    
    int main() {
        using namespace Shared;
        Mem mmf(bip::open_or_create, "./circ_buffer.bin", 10U << 10);
    
        auto& buf = *mmf.find_or_construct<StringBuf>("some_name")(10, mmf.get_segment_manager());
    
        for (auto& s : buf) {
            std::cout << "Existing " << std::quoted(std::string_view(s)) << "\n";
        }
    
        for (char const* init : {"foo", "bar", 
                "some pretty long string to make sure we don't fall into SSO territory"
                "some pretty long string to make sure we don't fall into SSO territory"
                "some pretty long string to make sure we don't fall into SSO territory"
                "some pretty long string to make sure we don't fall into SSO territory"
            })
        {
            buf.push_back(String(init, mmf.get_segment_manager()));
        }
    }
    

    On the second run prints:

    Existing "foo"
    Existing "bar"
    Existing "some pretty long string to make sure we don't fall into SSO territorysome pretty long string to make sure we don't fall into SSO territorysome pretty long string to make sure we don't fall into SSO territorysome pretty long string to make sure we don't fall into SSO territory"