Squadrick / shadesmar

Fast C++ IPC using shared memory
MIT License
555 stars 85 forks source link

Can it handle when one of the properties of the message passed is std::string? #69

Open zeroRains opened 4 months ago

zeroRains commented 4 months ago

I try to send a std::string from client to server. I modify the code in the rpc.cpp

I modify the struct Message,

struct Message {
  uint64_t count;
  uint64_t timestamp;
  uint8_t *data;
  std::string code;
};

function callback(),

bool callback(const shm::memory::Memblock &req, shm::memory::Memblock *resp) {
  auto *msg = reinterpret_cast<Message *>(req.ptr);
  auto lag = shm::current_time() - msg->timestamp;
  std::cout << current_count << ", " << msg->count <<"    "<<msg->code << "\n";
  ....
}

and function client_loop().

void client_loop(int seconds, int vector_size) {
  ...
  auto *rawptr = malloc(vector_size);
  std::memset(rawptr, 255, vector_size);
  Message *msg = reinterpret_cast<Message *>(rawptr);
  msg->count = 0;
  msg->code = "hello world!";
  ...
}

But it got a segmentation fault error when I std::cout the msg->code.