c++ - 发送字符串时 boost 消息队列崩溃

标签 c++ boost boost-interprocess

我正在尝试使用 boost interprocess message_queue 将字符串内容传递到另一个进程。当我传递整数时没有问题,但是当我想传递 std::string 底层的缓冲区时,会抛出异常

boost::interprocess_exception::library_error

代码如下。 这次崩溃的原因可能是什么?

  // SENDER process
  //
  message_queue::remove("messagequeue");

  //Create a message_queue.
  message_queue mq (create_only,"messagequeue",100,sizeof(char));
  std::string text("ciao");
  mq.send(text.data(), text.size(), 0);
  sleep(100);


  // RECV process
  //
  void get()
  {
    message_queue mq(open_only,"messagequeue");
    unsigned int priority;
    message_queue::size_type recvd_size;

    std::string message;
    message.resize(100);
    mq.receive(&message[0], 100, recvd_size, priority);
    message.resize(recvd_size);
    std::cout << message << std::endl;

    sleep(100000);
  }

  int main()
  {
    boost:: thread t;
    t = boost::thread(get);
    t.join();
    return 1;
  }

最佳答案

在创建消息队列的地方,您有 sizeof( char ) 但您试图在缓冲区中放入 4 个字符。跟踪谩骂异常位于:

   if (buffer_size > p_hdr->m_max_msg_size) {
      throw interprocess_exception(size_error);
   }

在message_queue.hpp中。与:

bi::message_queue mq( bi::open_or_create,"messagequeue", 100, 4 );

它适用于“ciao”。

关于c++ - 发送字符串时 boost 消息队列崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39636173/

相关文章:

c++ - 尝试跨多个进程使用 Boost.Interprocess 消息队列时的断言

c++ - MPI 代码在超过 2 个节点/进程的最终确定时挂起

c# - 什么是 IWebBrowser::Navigate2 等同于 window.open(...)?

c++ - 如何将指针从 unique_ptr 传递到另一个对象并管理生命周期?

c++ - 为什么 boost::make_shared 使用复制语义

c++ - 当 boost 库 "interprocess"定义一个 named_mutex 时,这些 named_mutex 是否在不同进程之间正常工作,或者只与线程一起工作?

c++ - 使用单写入器增长 Boost.Interprocess 内存映射文件

c++ - PDCurses getch 不起作用

c++ - 使用boost从json文件中的数组中解析元素

c++ - 有没有办法使用 Boost Test 测试非类型模板?