共享内存的std::string给分割错误(linux)

shared memory of std::string give segmentation fault (linux)

本文关键字:错误 linux 分割 string 内存 std 共享      更新时间:2023-10-16

我目前正在linux上的2个进程之间的共享内存中尝试放置结构。我没有问题共享bool或int,但当试图共享字符串,std::string或char我有一个分割错误错误。

现在我的代码是:

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

在这个例子中,从两个结构体中共享两个bool值工作得很好,但是如果我尝试从std::string (etat, carte)中输入数据或读取数据,就像这样:

PrisesArray[1].etat="hello";

它在调试中给了我一个分段错误(在发布中明确不工作),我尝试了简单的字符串和字符(甚至一个字符),它仍然给了我一个分段错误。

是我错过了什么,当谈到文本共享或犯了一个错误在这里?

它给了我一个分段错误在调试(和明确不工作释放),我尝试了简单的字符串和字符(甚至一个字符)和它仍然给我一个分割错误。

这是因为std::string不是POD (Plain Old Data)类型。string在后台执行动态内存分配(使用new),当序列化它时(例如到共享内存或文件),只有指针被序列化。当反序列化(从共享内存或文件)到原始指针的内存可能不再存在时,这会使反序列化的字符串无法使用。

您必须编写一个函数,专门将字符串显式地序列化到共享内存中,就像标准操作符std::ostream operator>>(std::ostream&

您必须共享与std::字符串相关的数据,即

//服务器部分:

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 strLocaln";
std::cout << strLocal << std::endl;
....
}