我的目标是创建一个名为SharedMemory的模板单例类,它可以使用boost :: interprocess :: managed_shared_memory将给定的数据结构存储在共享内存中的映射中。
#ifndef SHARED_MEMORY_H_
#define SHARED_MEMORY_H_
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <functional>
#include <string>
#include <utility>
#include <map>
using namespace boost::interprocess;
template<typename T>
class SharedMemory {
typedef std::pair<std::string, T> ValueType;
typedef allocator<ValueType, managed_shared_memory::segment_manager>
ShmemAllocator;
typedef map<std::string, T, std::less<std::string>, ShmemAllocator> SharedMap;
public:
static SharedMemory<T>& instance();
void Create();
void Destory();
void insert(T* t);
std::map<std::string, T> getMapOfRecords();
private:
SharedMemory();
~SharedMemory(){delete m_segment;}
void Initialize();
managed_shared_memory* m_segment;
std::size_t m_size;
};
template<typename T>
inline void SharedMemory<T>::Create() {
Destory();
m_segment = new managed_shared_memory(create_only, T::memory(), m_size);
ShmemAllocator alloc_inst (m_segment->get_segment_manager());
m_segment->construct<SharedMap>("SageMap")(std::less<std::string>(), alloc_inst);
}
template<typename T>
inline void SharedMemory<T>::Destory() {
shared_memory_object::remove(T::memory());
}
template<typename T>
inline SharedMemory<T>& SharedMemory<T>::instance() {
static SharedMemory<T> instance;
return instance;
}
template<typename T>
inline SharedMemory<T>::SharedMemory()
: m_size(65536) {
}
template<typename T>
inline void SharedMemory<T>::insert(T* t) {
SharedMap* mymap = m_segment->find<SharedMap>("SageMap").first;
mymap->insert(std::pair<std::string, T>(t->key(), *t));
}
template<typename T>
inline std::map<std::string, T> SharedMemory<T>::getMapOfRecords() {
SharedMap* mymap = m_segment->find<SharedMap>("SageMap").first;
return std::map<std::string, T>(mymap->begin(), mymap->end());
}
#endif
以下是如何使用它的示例。
#include <boost/lexical_cast.hpp>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <functional>
#include <utility>
#include "SharedMemory.hpp"
struct simple_type {
int i;
std::string key() {return boost::lexical_cast<std::string>(i);}
static const char* memory() {return std::string("simple_memory_page").c_str();}
simple_type(int i): i(i){}
};
int main(int argc, char *argv[])
{
if(argc == 1) {
SharedMemory<simple_type>& test = SharedMemory<simple_type>::instance();
test.Create();
test.insert(new simple_type(1));
test.insert(new simple_type(2));
std::string s(argv[0]); s += " child ";
if(0 != std::system(s.c_str()))
return 1;
test.Destory();
} else {
SharedMemory<simple_type>& test = SharedMemory<simple_type>::instance();
std::map<std::string, simple_type> records = test.getMapOfRecords();
for(auto it = records.begin(); it != records.end(); ++it) {
std::cout << it->second.i << std::endl;
}
}
return 0;
}
这是一个堆栈跟踪:
position_monitor_eu.exe!boost::interprocess::offset_ptr<boost::intrusive::compact_rbtree_node<boost::interprocess::offset_ptr<void> > >::get_pointer() Line 81 + 0x1a bytes C++
position_monitor_eu.exe!boost::interprocess::offset_ptr<boost::intrusive::compact_rbtree_node<boost::interprocess::offset_ptr<void> > >::get() Line 153 + 0x16 bytes C++
position_monitor_eu.exe!boost::interprocess::offset_ptr<boost::intrusive::compact_rbtree_node<boost::interprocess::offset_ptr<void> > >::offset_ptr<boost::intrusive::compact_rbtree_node<boost::interprocess::offset_ptr<void> > >(const boost::interprocess::offset_ptr<boost::intrusive::compact_rbtree_node<boost::interprocess::offset_ptr<void> > > & ptr={...}) Line 117 + 0x16 bytes C++
position_monitor_eu.exe!boost::intrusive::compact_rbtree_node_traits_impl<boost::interprocess::offset_ptr<void> >::get_left(boost::interprocess::offset_ptr<boost::intrusive::compact_rbtree_node<boost::interprocess::offset_ptr<void> > const > n={...}) Line 124 + 0x17 bytes C++
position_monitor_eu.exe!boost::intrusive::rbtree_impl<boost::intrusive::setopt<boost::intrusive::detail::base_hook_traits<boost::container::containers_detail::rbtree_node<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,simple_type>,boost::interprocess::offset_ptr<void> >,boost::intrusive::rbtree_node_traits<boost::interprocess::offset_ptr<void>,1>,0,boost::intrusive::default_tag,3>,boost::container::containers_detail::node_compare<boost::container::containers_detail::value_compare_impl<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,simple_type>,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,boost::container::containers_detail::select1st<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,simple_type> > >,boost::container::containers_detail::rbtree_node<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,simple_type>,boost::interprocess::offset_ptr<void> > >,unsigned int,1> >::begin() Line 273 + 0x42 bytes C++
position_monitor_eu.exe!boost::container::containers_detail::rbtree<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,simple_type>,boost::container::containers_detail::select1st<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,simple_type> >,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,boost::interprocess::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,simple_type>,boost::interprocess::segment_manager<char,boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family,boost::interprocess::offset_ptr<void>,0>,boost::interprocess::iset_index> > >::begin() Line 493 + 0x28 bytes C++
position_monitor_eu.exe!boost::container::map<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,simple_type,std::less<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >,boost::interprocess::allocator<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,simple_type>,boost::interprocess::segment_manager<char,boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family,boost::interprocess::offset_ptr<void>,0>,boost::interprocess::iset_index> > >::begin() Line 245 + 0x1a bytes C++
position_monitor_eu.exe!SharedMemory<simple_type>::getMapOfRecords() Line 68 + 0x1e bytes C++
position_monitor_eu.exe!main(int argc=2, char * * argv=0x02b03db8) Line 200 + 0xc bytes C++
position_monitor_eu.exe!__tmainCRTStartup() Line 555 + 0x19 bytes C
position_monitor_eu.exe!mainCRTStartup() Line 371 C
kernel32.dll!77003677()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!775cc002()
ntdll.dll!775cbfd5()
我目前的问题是,mymap->begin()
中的getMapOfRecords()
来电,程序崩溃了。
答案 0 :(得分:5)
看起来你将std::string
类型放在共享内存中,这将无法工作,因为它会为自己分配非共享内存。您应该使用boost::interprocess::basic_string
来代替<boost/interprocess/containers/string.hpp>
。有一个将字符串放在地图here中的示例。
要回答您在上面的评论中提到的内容,string::c_str()
返回的值在字符串被销毁后变为无效。这意味着访问memory()
返回的指针将导致未定义的行为。
答案 1 :(得分:1)
要添加interjay写的关于string :: c_str()的内容,在第一次非常量成员调用字符串对象后,返回的值不保证有效。因此,对string :: resize的后续调用可能会使string :: c_str()返回的值无效。