Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9156351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:45:23+00:00 2026-06-17T12:45:23+00:00

I have a class named GenericMessage shown in the 1st code snippet below (defined

  • 0

I have a class named GenericMessage shown in the 1st code snippet below (defined in GenericMessage.hxx).

I have a .cpp file named TestFE.cpp (see the 2nd code snippet below) that attempts to send an instance of class GenericMessage via a ZMQ queue (See also the 4th code snippet very below – ZmqHandler.hxx). TesfFE.cpp implements the ZMQ push pattern here by including ZmqHandler.hxx.

I have yet another .cpp file named TestBE.cpp (see the 3rd code snippet below) that receives so mentioned GenericMessage instance via the ZMQ queue. TestBE.cpp implements the ZMQ pull pattern here to retrive the GenericMessage instance over the ZMQ queue.

In the TestFE.cpp, I use the standard memcpy function in order to convert the GenericMessage object into a form that can be accepted by the ZMQ queue. On the line 21 of TestBE.cpp (marked in the 3rd code snippet in comments), I get a segmentation fault because it looks the memcpy does not work properly on the sender side which is TestFE.cpp. I got the below message when TestBE is executed. I am also providing the gdb backtrace just below. Could you please tell me what’s wrong here? Why do you think memcpy cannot copy my GenericMessage object to ZMQ message_t format properly? Or do you think the problem is st else? Any comments would be appreciated.

ERROR MESSAGE

  $ ./TestBE
  Connecting to FE...
  RECEIVED: 1
  Segmentation fault (core dumped)

GDB Backtrace

  (gdb) r
   Starting program: /home/holb/HOLB_DESIGN/ZMQ/WORK1/TestBE 
  [Thread debugging using libthread_db enabled]
  Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
  [New Thread 0xb7c84b40 (LWP 4252)]
  [New Thread 0xb7483b40 (LWP 4253)]
  Connecting to FE...
  RECEIVED: 1

  Program received signal SIGSEGV, Segmentation fault.
  0xb7f371cc in std::basic_string<char, std::char_traits<char>, std::allocator<char>       >::basic_string(std::string const&) ()
  from /usr/lib/i386-linux-gnu/libstdc++.so.6
  (gdb) bt
  #0  0xb7f371cc in std::basic_string<char, std::char_traits<char>,      std::allocator<char> >::basic_string(std::string const&) ()
  from /usr/lib/i386-linux-gnu/libstdc++.so.6
  #1  0x08049621 in GenericMessage<std::string>::getData (this=0xbffff06c)
  at GenericMessage.hxx:18
  #2  0x08049075 in main () at TestBE.cxx:21
 (gdb) 

CODE SNIPPET 1 (GenericMessage.hxx)
#include
#include
#include

  template <class T>
  class GenericMessage {
  public:

         GenericMessage(int id, T msg): 
         beId(id), 
         data(msg)
         {}

         ~GenericMessage(){}

         T getData()
         {
           //LINE 18 is the following line!
           return data;
         }

          std::string toString()
          {
              std::ostringstream ss;
              ss << getBeId();
              std::string ret =  ss.str();

              return ret;
          }

          void setBeId(int id)
          {
              beId = id;
          }

          int getBeId()
          {
             return beId;
          }
 private:
        int beId;
        T data;
 };

CODE SNIPPET 2 (TestFE.cxx ==> The sender)
#include “ZmqHandler.hxx”
//SEE THE 4th snippet at the bottom for the content of ZmqHandler.hxx

 int main ()
 {
     ZmqHandler<std::string> zmqHandler;
     int counter = 1;

     while(1)
     {  
        std::string data = "Hello there!\0";
        GenericMessage<std::string> msg(counter, data);
        zmqHandler.sendToBE(&msg);
        counter++;
        sleep(1);
     }

     return 0;
 }

CODE SNIPPET 3 (TestBE.cxx ==> The receiver)

  #include "zmq.hpp"
  #include "GenericMessage.hxx"
  #include <string>
  #include <iostream>

  int main ()
  {
      //  Prepare our context and socket
      zmq::context_t context (1);
      zmq::socket_t socket (context, ZMQ_PULL);

     std::cout << "Connecting to FE..." << std::endl;
     socket.connect ("tcp://localhost:5555");

    while(1){
        zmq::message_t reply;
            socket.recv (&reply);
            GenericMessage<std::string> *msg = (GenericMessage<std::string>*)(reply.data());                
            std::cout << "RECEIVED: " << msg->toString() << std::endl;

           /* *********************************  */
           /*  SEGMENTATION FAULT HAPPENS HERE   */
           /* The member "data" in class GenericMessage cannot be received while the  member "id" in the previous line can be received. */
           std::cout << "DATA: " << ((std::string)msg->getData())  << std::endl;
           /* ********************************** */
     }

     return 0;
 }

CODE SNIPPET 4 (ZMQHandler.hxx)

  #include "zmq.hpp"  
  #include "GenericMessage.hxx"
  #include <pthread.h>
  #include <unistd.h>
  #include <cassert>

  template <class T>
  class ZmqHandler {
  public:
        ZmqHandler():
    mContext(1),
    mOutbHandlerSocket(mContext, ZMQ_PUSH)
        {    
          mOutbHandlerSocket.bind ("tcp://*:5555");       
        }

       ~ZmqHandler() {}

       void *sendToBE(GenericMessage<T> *theMsg)
       {
         //  Place the new request to the zmq queue for BE consumption
         zmq::message_t msgToSend(sizeof(*theMsg));

         memcpy ( msgToSend.data(), ((GenericMessage<T>*)theMsg), sizeof(*  ((GenericMessage<T>*)theMsg)));

         mOutbHandlerSocket.send(msgToSend);

         std::cout << "SENT request: [" << theMsg->toString() << "]" << std::endl;

         return (NULL);
        }   

   private:  
        zmq::context_t mContext;
        zmq::socket_t mOutbHandlerSocket; 

};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:45:25+00:00Added an answer on June 17, 2026 at 12:45 pm

    I’m beginning to see the problem. It’s that you send the complete “structure”, which contains a member variable that have pointers (the std::string). This you can not do, as pointers are only valid in the program that created them.

    You have to serialize the structure before sending it, and then de-serialize on the receiving end.

    You can use libraries such as Boost serialization for this, or Google protocol buffers, or any other number of libraries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class named MyClass. And in the file MyClass.m I start the
I have a class named Defense with custom init method as below: // initialize
I have Class named User, code: Public Class User Private m_id As String Private
I have a class named Model and in ypur .h file I have this:
Description: I have a C++ class named GenericMessage which simply holds an id and
I have a Class named Mo. Mo has a static function as below: public
I have a class named CreateListView in my project in a .cs file and
I have a class named Channel with two methods defined: class Channel { void
I have a class named myClass. This class has TouchesMoved and TouchesBegan defined. This
i have class named Group i tried to test configuration: var cfg = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.