[英]shared memory of std::string give segmentation fault (linux)
I am currently trying the put structures in a shared memory between 2 process on linux.我目前正在尝试在 linux 上的 2 个进程之间的共享内存中放置结构。 I have no problem sharing bool or int but when trying to share a string, std::string or char i have a segmentation fault error.
我共享 bool 或 int 没有问题,但是在尝试共享字符串、std::string 或 char 时,我遇到了分段错误错误。
Right now my code is :现在我的代码是:
#include <iostream>
#include <sys/types.h> //shmat
#include <sys/shm.h>
#include <sys/stat.h> //open
#include <fcntl.h>
#include <unistd.h> //close
using namespace std;
struct Prises{
int numero;
int transactionId;
bool reservation;
bool charge;
bool disponibilite;
bool defaut;
bool verrouillage;
bool trappe;
int LEDverte;
int LEDrouge;
std::string carte;
std::string etat;
};
int main()
{
const char *keyFile = "/tmp/key.dat";
/* Make sure the file exists. */
int descriptor = open(keyFile, O_CREAT | O_RDWR, S_IRWXU);
/* Only wanted to make sure that the file exists. */
close(descriptor);
/* Generate memory key. */
key_t sharedKey = ftok(keyFile, 1);
/* Get the shared memory segment id. Note we include
the permissions. */
int sharedSpaceId = shmget(sharedKey, 2*sizeof(Prises),
IPC_CREAT | S_IRUSR | S_IWUSR);
/* Attach the shared memory segment. */
Prises *PrisesArray = (Prises *) shmat(sharedSpaceId, NULL, 0);
PrisesArray[1].defaut=true;
PrisesArray[2].defaut=false;
int ok;
std::cin>>ok;
return 0;
}
In this example sharing the 2 bool from the 2 structures is working well but if i try to input a data or read a data from the std::string (etat, carte) like this :在此示例中,共享 2 个结构中的 2 bool 运行良好,但如果我尝试输入数据或从 std::string (etat, carte) 中读取数据,如下所示:
PrisesArray[1].etat="hello";
It gives me a segmentation fault in debug (and clear don't work in release), i tried with simple string and char (even one char) and it still gives me a segmentation fault.它在调试中给了我一个分段错误(并且明确在发布中不起作用),我尝试使用简单的字符串和字符(甚至一个字符),但它仍然给我一个分段错误。
Am i missing something when it comes to text sharing or making a mistake here ?在文本共享或在这里犯错时,我是否遗漏了什么?
It gives me a segmentation fault in debug (and clear don't work in release), i tried with simple string and char (even one char) and it still gives me a segmentation fault.
它在调试中给了我一个分段错误(并且明确在发布中不起作用),我尝试使用简单的字符串和字符(甚至一个字符),但它仍然给我一个分段错误。
This is because std::string is not a POD (Plain Old Data) type.这是因为 std::string 不是 POD(Plain Old Data)类型。 std::string performs dynamic memory allocation (using new) behind the scenes, and when serializing it (eg to shared memory or file), only the pointers are serialized.
std::string 在幕后执行动态内存分配(使用 new),并且在对其进行序列化(例如共享内存或文件)时,仅对指针进行序列化。 When deserializing it (from shared memory or file) memory to the original pointers are likely to not exist anymore, and this renders the deserialized string unusable.
当将它(从共享内存或文件)反序列化时,原始指针的内存可能不再存在,这使得反序列化的字符串无法使用。
You would have to write a function that specifically serializes the string explicitly to your shared memory, as is the case with the standard operator std::ostream operator >>(std::ostream& stream, const std::string&)您必须编写一个专门将字符串显式序列化到共享内存的函数,就像标准运算符std::ostream operator >>(std::ostream& stream, const std::string&) 一样
You have to share the data associated with the std::string, ie您必须共享与 std::string 关联的数据,即
//server part: //服务器部分:
struct ShmData{
`...`
char chAux[1024];
};
int main()
{
...
shared_memory_object shm (create_only, "MyMem", read_write);
shm.truncate(offset_t(sizeof(ShmData)) );
mapped_region region(shm, read_write);
ShmData *shmData = (ShmData*)(region.get_address());
...
std::string str( "some stuff over the server" );
...
strcpy( shmData->chAux, str.c_str() );
...
}
//on client, the same struct and:
int main()
{
shared_memory_object shm (open_only, "MyMem", read_write);
mapped_region region(shm, read_write);
ShmData *shmData = (ShmData*)(region.get_address());
std::string strLocal(shmData->chAux); //assign to a local string the shared data
std::cout << "show strLocal\n";
std::cout << strLocal << std::endl;
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.