Boost 托管\u共享\u内存中带有字符串的无序\u映射失败

Boost 托管\u共享\u内存中带有字符串的无序\u映射失败,boost,unordered-map,interprocess,Boost,Unordered Map,Interprocess,这是我的代码: int main (int argc, char *argv[]) { typedef int KeyType; typedef string MappedType; typedef std::pair<KeyType, MappedType> ValueType; typedef boost::interprocess::allocator<ValueType, boost::interprocess::managed_sha

这是我的代码:

int main (int argc, char *argv[])
{
    typedef int KeyType;
    typedef string MappedType;

    typedef std::pair<KeyType, MappedType> ValueType;
    typedef boost::interprocess::allocator<ValueType, boost::interprocess::managed_shared_memory::segment_manager> ShmAlloc;
    typedef boost::unordered_map<KeyType, MappedType, boost::hash<KeyType>, std::equal_to<KeyType>, ShmAlloc> ShmHashMap;

    boost::interprocess::managed_shared_memory segment(boost::interprocess::open_or_create, "ContainerSharedMemory", 65536);

    if(argc == 2 && string(argv[1]) == "clear")
    {
        boost::interprocess::shared_memory_object::remove("ContainerSharedMemory");
        return 0;
    }

    ShmHashMap *hash_map = segment.find_or_construct<ShmHashMap>(boost::interprocess::unique_instance)(segment.get_segment_manager());

    if(hash_map == NULL)
    {
        cout << "find_or_construct error" << endl;
        return 0;
    }

    for(int i = 0; i < 5; ++i) {
        ShmHashMap::iterator iter = hash_map->find(i);
        if (iter == hash_map->end()) {
            hash_map->insert(ValueType(i, "test"));
        }
    }

    cout << "all..." << endl;
    for(ShmHashMap::iterator iter = hash_map->begin(); iter != hash_map->end(); ++iter)
    {
        cout << iter->first << "|" << iter->second << endl;
    }
    cout << "end..." << endl;

    return 0;
}

当然<代码>标准::字符串从堆中分配

堆在您的进程地址空间中,因此读取相同共享内存的任何其他进程都将在那里获得错误的原始指针并调用UB

您还需要使用带有字符串的共享内存分配器

(使用Coliru的映射文件)

使用共享内存:

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/container/scoped_allocator.hpp>
#include <boost/container/string.hpp>
#include <boost/unordered_map.hpp>
#include <iostream>

namespace bip = boost::interprocess;

int main (int argc, char *argv[])
{
    typedef int KeyType;

    typedef boost::container::basic_string<char, std::char_traits<char>, bip::allocator<char, bip::managed_shared_memory::segment_manager> > MappedType;

    typedef std::pair<KeyType, MappedType> ValueType;
    typedef boost::interprocess::allocator<ValueType, boost::interprocess::managed_shared_memory::segment_manager> ShmAlloc;
    typedef boost::unordered_map<KeyType, MappedType, boost::hash<KeyType>, std::equal_to<KeyType>, boost::container::scoped_allocator_adaptor<ShmAlloc> > ShmHashMap;

    boost::interprocess::managed_shared_memory segment(boost::interprocess::open_or_create, "ContainerSharedMemory", 65536);

    if(argc == 2 && std::string(argv[1]) == "clear")
    {
        boost::interprocess::shared_memory_object::remove("ContainerSharedMemory");
        return 0;
    }

    ShmHashMap *hash_map = segment.find_or_construct<ShmHashMap>(boost::interprocess::unique_instance)(segment.get_segment_manager());

    if(hash_map == NULL)
    {
        std::cout << "find_or_construct error" << std::endl;
        return 0;
    }

    for(int i = 0; i < 5; ++i) {
        ShmHashMap::iterator iter = hash_map->find(i);
        if (iter == hash_map->end()) {
            hash_map->insert(ValueType(i, MappedType { "hello", segment.get_segment_manager() }));
        }
    }

    std::cout << "all..." << std::endl;
    for(ShmHashMap::iterator iter = hash_map->begin(); iter != hash_map->end(); ++iter)
    {
        std::cout << iter->first << "|" << iter->second << std::endl;
    }
    std::cout << "end..." << std::endl;
}

如何为字符串分配分配器,请向我展示一些细节,谢谢。有些人有工作要做。抱歉耽搁了(顺便说一句,我有很多答案,这些东西已经在网站上了,你可以通过搜索来帮助自己)无法粘贴代码。。。如果我像这样封装一个模板类:模板类MMSHashMap。。。。如何将其设计为隐藏分配器detailAh。请看我现有的答案。如果找不到,将稍后为您搜索。如果您有新问题,请发布新问题。这可能是一个重复的一个我链接好,我会读你的答案在该链接第一…谢谢你~
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/container/scoped_allocator.hpp>
#include <boost/container/string.hpp>
#include <boost/unordered_map.hpp>
#include <iostream>

namespace bip = boost::interprocess;

int main (int argc, char *argv[])
{
    typedef int KeyType;

    typedef boost::container::basic_string<char, std::char_traits<char>, bip::allocator<char, bip::managed_shared_memory::segment_manager> > MappedType;

    typedef std::pair<KeyType, MappedType> ValueType;
    typedef boost::interprocess::allocator<ValueType, boost::interprocess::managed_shared_memory::segment_manager> ShmAlloc;
    typedef boost::unordered_map<KeyType, MappedType, boost::hash<KeyType>, std::equal_to<KeyType>, boost::container::scoped_allocator_adaptor<ShmAlloc> > ShmHashMap;

    boost::interprocess::managed_shared_memory segment(boost::interprocess::open_or_create, "ContainerSharedMemory", 65536);

    if(argc == 2 && std::string(argv[1]) == "clear")
    {
        boost::interprocess::shared_memory_object::remove("ContainerSharedMemory");
        return 0;
    }

    ShmHashMap *hash_map = segment.find_or_construct<ShmHashMap>(boost::interprocess::unique_instance)(segment.get_segment_manager());

    if(hash_map == NULL)
    {
        std::cout << "find_or_construct error" << std::endl;
        return 0;
    }

    for(int i = 0; i < 5; ++i) {
        ShmHashMap::iterator iter = hash_map->find(i);
        if (iter == hash_map->end()) {
            hash_map->insert(ValueType(i, MappedType { "hello", segment.get_segment_manager() }));
        }
    }

    std::cout << "all..." << std::endl;
    for(ShmHashMap::iterator iter = hash_map->begin(); iter != hash_map->end(); ++iter)
    {
        std::cout << iter->first << "|" << iter->second << std::endl;
    }
    std::cout << "end..." << std::endl;
}
all...
4|hello
3|hello
2|hello
1|hello
0|hello
end...