我正在使用 boost::interprocess 在进程之间共享对象。 我有两个文件,一个生成结构对象并将该对象传递到具有 int 索引的映射中的“server.cpp”;和一个“client.cpp”文件,它检索内存数据并遍历数据,输出到控制台。
结构看起来像这样:
struct mydata o {
string MY_STRING;
int MY_INT;
};
和对象:
mydata o;
o.MY_STRING = "hello";
o.MY_INT = 45;
服务器和客户端都能正确编译。 但是出于某种原因,如果我尝试访问客户端中的字符串而不是 float 或整数,客户端可执行文件会抛出段错误。 例如下面的 second.MY_INT 将输出到控制台,但 second.MY_STRING 在运行时抛出此错误。
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <functional>
#include <utility>
#include <iostream>
#include <string>
#include "objects.cpp" //definitions for objects
using std::string;
using namespace boost::interprocess;
int main ()
{
try
{
managed_shared_memory segment(open_or_create, "SharedMemoryName",65536);
//Again the map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>, so the allocator must allocate that pair.
typedef int KeyType;
typedef order MappedType;
typedef std::pair<const int, order> ValueType;
//Assign allocator
typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
//The map
typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MySHMMap;
//Initialize the shared memory STL-compatible allocator
ShmemAllocator alloc_inst (segment.get_segment_manager());
//access the map in SHM through the offset ptr
MySHMMap :: iterator iter;
offset_ptr<MySHMMap> m_pmap = segment.find<MySHMMap>("MySHMMapName").first;
iter=m_pmap->begin();
for(; iter!=m_pmap->end();iter++)
{
//std::cout<<"\n "<<iter->first<<" "<<iter->second;
std::cout<<iter->first<<" "<<iter->second.MYINT<<" "<<iter->second.MYSTRING<<"\n";
}
}catch(std::exception &e)
{
std::cout<<" error " << e.what() <<std::endl;
shared_memory_object::remove("SharedMemoryName");
}
return 0;
}
运行时报错:
Segmentation fault (core dumped)
我很确定服务器正在将整个对象传递到内存,并且客户端可以检索它(因为我可以访问一些对象属性),这只是一个格式问题。
最佳答案
就像贾斯汀提到的,std::string本身就是一个动态分配的容器。
仅使用 Interprocess 的 string是不足够的。事实上,那只是boost::container::basic_string<>真的。
重要的是使用分配器。
但是,使用带有分配器的映射,并在需要构建包含的容器时传递分配器(令人作呕)是很烦人的。
这样一来,您就不必知道分配器,任何知道如何使用作用域分配器的容器都会将分配器传递给嵌套容器。
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/container/scoped_allocator.hpp>
#include <iostream>
#include <string>
namespace bip = boost::interprocess;
namespace Shared {
using Segment = bip::managed_shared_memory;
using Manager = Segment::segment_manager;
template <typename T> using Alloc
= boost::container::scoped_allocator_adaptor<bip::allocator<T, Manager> >;
using String = bip::basic_string<char, std::char_traits<char>, Alloc<char> >;
template <typename K, typename V, typename Cmp = std::less<K> > using Map
= bip::map<K, V, Cmp, Alloc<std::pair<K const, V> > >;
struct Order {
using allocator_type = Alloc<char>;
template <typename S, typename Alloc>
Order(int i, S const& s, Alloc alloc = {}) : i(i), s(s, alloc) {}
int i;
String s;
};
}
int main() {
try {
using namespace Shared;
Segment segment(bip::open_or_create, "095540a3-ceaa-4431-828d-df21d5e384ae", 65536);
auto& pmap = *segment.find_or_construct<Map<int, Order>>("MySHMMapName")(segment.get_segment_manager());
if (pmap.empty()) {
std::cout << "Inserting data\n";
auto insert = [&pmap](int i, auto&& s) {
using namespace std;
pmap.emplace(piecewise_construct, tie(i), tie(i, s));
};
insert(1, "one");
insert(2, "two");
insert(3, "three");
} else {
std::cout << "Existing data:\n";
for (auto& [k,v] : pmap) {
std::cout << k << " " << v.i << " " << v.s << "\n";
}
}
} catch (std::exception &e) {
std::cout << " error " << e.what() << std::endl;
bip::shared_memory_object::remove("095540a3-ceaa-4431-828d-df21d5e384ae");
}
}
我注意到 map 是 Map<int, Order> : key 似乎复制了 Order 中的整数值.
Map<int, String> 你可以做到 Map<int, String>并获得更流畅的体验(因为不需要 std::piecewise_construct ):
auto& pmap = *segment.find_or_construct<Map<int, String>>("MySHMMapName")(segment.get_segment_manager());
if (pmap.empty()) {
std::cout << "Inserting data\n";
pmap.emplace(1, "one");
pmap.emplace(2, "two");
pmap.emplace(3, "three");
}
或者,您应该考虑使用能够索引 Order 的多索引直接由类型的成员:
namespace bmi = boost::multi_index;
using Table = bmi::multi_index_container<Order,
bmi::indexed_by<
bmi::ordered_unique< bmi::member<Order, int, &Order::i> >
>,
Alloc<Order>
>;
遗憾的是,Multi Index 不能很好地处理使用分配器的嵌套类型,因此您必须再次传递它:
if (pmap.empty()) {
std::cout << "Inserting data\n";
pmap.emplace(1, "one", pmap.get_allocator());
pmap.emplace(2, "two", pmap.get_allocator());
pmap.emplace(3, "three", pmap.get_allocator());
} else {
std::cout << "Existing data:\n";
for (Order const& o : pmap) {
std::cout << o.i << " " << o.s << "\n";
}
// demonstrate lookup:
std::cout << "Finding element 2:" << pmap.find(2)->s << "\n";
}
打印
Existing data:
1 one
2 two
3 three
Finding element 2:two
¹ 在 Coliru 上使用映射文件。重构后的代码仅需一行更改。
关于c++ - boost::interprocess : cout a string variable when iterating through a map that references an object from a struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48675001/