unordered_map with string in managed_shared_memory fails

☆樱花仙子☆ 提交于 2020-01-06 03:08:30

问题


This is my code:

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;
}

Everything is ok when MappedType is int, but a segment fault whit this code like this:

Rerun this program to access hash map in shared memory will coredump

----------------------------edit again----------------------------------

the problem about string is solved by sehe, thank you and if i design a template class want to hide that detail, how could i do? if there is some perfect way

template<typename MappedType>
struct ComplexMappedType
{
    ComplexMappedType(): t_access(0), t_expire(0) {}
    ComplexMappedType(const MappedType& v, uint32_t a, uint32_t e): value(v), t_access(a), t_expire(e) {}
    MappedType value;
     uint32_t t_access;
     uint32_t t_expire;
 };

 template <typename KeyType, typename MappedType>
 class MMSHashMap
 {
 private:
    typedef ComplexMappedType<MappedType> DataType;
    typedef std::pair<KeyType, DataType> ValueType;
    typedef boost::interprocess::allocator<ValueType, boost::interprocess::managed_shared_memory::segment_manager> ShmAlloc;
    typedef boost::unordered_map<KeyType, DataType, boost::hash<KeyType>, std::equal_to<KeyType>, ShmAlloc> ShmHashMap;

public:
    MMSHashMap(const std::string& name, size_t size, float e_thr, float e_scale);
    ~MMSHashMap() {delete pMemorySegment;}

    size_t getMEMSize() { return pMemorySegment->get_size(); }
    size_t getMEMFreeSize() { return pMemorySegment->get_free_memory(); }

    bool get(const KeyType& key, MappedType& value, uint32_t& expire);
    bool set(const KeyType& key, const MappedType& value, uint32_t expire);
    bool del(const KeyType& key);

private:
    void doCapacityElimination();

    std::string _name;
    boost::interprocess::managed_shared_memory* pMemorySegment;
    boost::shared_mutex mutex, mutex_eliminate;
    float fEliminateThreshold, fEliminateScale;
};

回答1:


Of course. std::string allocates from the heap.

The heap is in your process address space, so any other process reading the same shared memory is going to get a wrong raw pointer there and invoke UB.

You need to use a shared-memory allocator with the strings too.

Live On Coliru (using mapped file for Coliru)

With shared memory:

#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;
}

Prints

all...
4|hello
3|hello
2|hello
1|hello
0|hello
end...


来源:https://stackoverflow.com/questions/36831496/unordered-map-with-string-in-managed-shared-memory-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!