问题描述
我正在使用boost :: interprocess在进程之间共享对象.我有两个文件,一个"server.cpp",它生成一个struct对象并将该对象传递给具有int索引的映射;还有一个"client.cpp"文件,用于检索内存数据并遍历数据,并输出到控制台.
I'm using boost::interprocess to share objects between processes.I have two files, a "server.cpp" that generates a struct object and passes the object into a map with an int index; and a "client.cpp" file that retrieves the memory data and iterates through the data, outputting to console.
结构如下:
struct mydata o {
string MY_STRING;
int MY_INT;
};
对象:
mydata o;
o.MY_STRING = "hello";
o.MY_INT = 45;
服务器和客户端均可正确编译.但是由于某种原因,如果我尝试访问客户端中的字符串而不是浮点数或整数,则客户端可执行文件会引发分段错误.例如,下面的second.MY_INT将退出控制台,但是second.MY_STRING在运行时会引发此错误.
Both server and client compile correctly.But for some reason the client executable throws a segmentation fault if I try to access a string rather than a float or an integer in the client.For example the below second.MY_INT will cout to console, but second.MY_STRING throws this error when running.
#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)
我很确定服务器正在将整个对象传递到内存中,并且客户端可以检索它(因为我可以访问某些对象属性),而这仅仅是一个格式化问题.
I'm pretty sure the server is passing the entire object to memory, and the client can retrieve it (as i can access some of the objects attributes), and that its just a formatting issue.
推荐答案
以前海报中的一些重要信息使我想到了自己的简单答案.在结构中使用char数组,然后将其读取为字符串,如下所示:
Some great info from previous posters led me to a simple answer of my own. Use a char array in the struct, then read it into a string as follows:
std::cout<<iter->first<<" "<<iter->second.ORDERTYPE<<" "<<string(iter->second.MYID)<<"\n";
这是我所说的结构:
struct order {
char MYID[100];
int ORDERTYPE;
char DATE_UPDATED[64];
};
order o
这是我将结构传递到内存中的方式:
here is how i passed the struct into memory:
m_pmap->insert(std::pair<const int, order>(i, (order)o));
现在,我可以将包含字符串(包括字符串)在内的各种类型的结构映射写入内存,并从内存中检索它们.
now i am able to write a map of structs containing various types including strings, to memory, and retrieve them from memory.
这篇关于Boost进程间:遍历通过引用结构体中的对象的映射时,引用字符串变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!