C++ 循环\u缓冲区和托管\u映射\u文件分段错误

C++ 循环\u缓冲区和托管\u映射\u文件分段错误,c++,boost,memory-mapped-files,interprocess,circular-buffer,C++,Boost,Memory Mapped Files,Interprocess,Circular Buffer,我正在使用boost 1.73.0,并尝试将循环缓冲区与管理映射文件一起用于将字符串存储在磁盘上持久化的循环缓冲区中 我执行以下操作来创建/打开循环缓冲区: boost::interprocess::managed_mapped_file mmf(boost::interprocess::open_or_create, "./circ_buffer.bin", 10u << 10); typedef boost::interprocess::allocator&l

我正在使用boost 1.73.0,并尝试将循环缓冲区与管理映射文件一起用于将字符串存储在磁盘上持久化的循环缓冲区中

我执行以下操作来创建/打开循环缓冲区:

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());
我有一个分割错误。 我知道最终我需要围绕内存访问进行同步,但这不应该是上面示例中的问题,因为在任何给定时间只有一个进程在访问文件

有趣的是,如果在分配器中用char替换std::string,则不会出现分段错误。 我做错了什么

Rgds 克劳斯

在第二次运行时,打印:

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"
增加了固定演示
instance->front()
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());
#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()));
    }
}
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"