[英]Boost: De-serializing a custom C++ object passed over ZeroMQ pull socket
The text I am writing here is a follow up problem that I opened up earlier in another thread at Boost: Serializing/De-serializing a custom C++ object passed over ZeroMQ pull socket . 我在此处编写的文本是我在Boost的另一个线程中先前提出的跟进问题:序列化/反序列化通过ZeroMQ pull socket传递的自定义C ++对象 。 The compilation problems in the earlier thread have been resolved by using a textarchive type rather than binaryarchive but now I experience a runtime problem when de-serializing. 早期线程中的编译问题已通过使用textarchive类型而不是binaryarchive解决,但现在在反序列化时遇到了运行时问题。 I repeat the earlier text here with a new problem statement for your convenience. 为了方便起见,我在此处重复前面的内容并添加新的问题说明。 I am relatively new to C++ realm and appreciate any further help. 我是C ++领域的新手,并希望获得更多帮助。
Description: 描述:
I have a C++ class named GenericMessage which simply holds an id and data as its members (See code snippet 2 below - GenericMessage.hxx). 我有一个名为GenericMessage的C ++类,它仅将ID和数据作为其成员(请参见下面的代码段2-GenericMessage.hxx)。 My intention is to serialize an instance of this class and send it via a ZeroMQ socket which implements the push pattern. 我的意图是序列化此类的实例,并通过实现推模式的ZeroMQ套接字发送它。
The serialization and sending task has been implemented in class ZMQHandler (see sendToBE function) which is placed in a header file name ZMQHandler.hxx shown in the code snippet 3 below. 序列化和发送任务已在类ZMQHandler (请参见sendToBE函数)中实现, 该类放置在头文件名称ZMQHandler.hxx中,如下面的代码片段3所示。 This class is instantiated by TestFE.cxx shown in the 4rd code snippet below. 此类由下面的第4个代码段中所示的TestFE.cxx实例化。
The receiving and de-serialization of the GenericMessage instance is implemented in TestBE.cxx available in the 5th code snippet below. GenericMessage实例的接收和反序列化在下面第5个代码段中提供的TestBE.cxx中实现。 My intention is to receive the GenericMessage instance over the ZMQ socket (ie pull socket), de-serialize it and then print its members to standard output. 我的目的是通过ZMQ套接字(即拉套接字)接收GenericMessage实例,对其进行反序列化,然后将其成员打印到标准输出中。
Problem Statement: 问题陈述:
When I run the receiver (ie TestBE.cxx) I verified here that I can transfer data from TestFE.cxx to TEstBE.cxx over the ZMQ socket. 当我运行接收器(即TestBE.cxx)时,我在这里验证了可以通过ZMQ套接字将数据从TestFE.cxx传输到TEstBE.cxx。 However, I got an exception shown in the 1st code snippet below exactly when I attempt to de-serialize the input archive at line 28 in TestBE.cxx (see the 5th code snippet very below, line 28 has been marked). 但是,当我尝试在TestBE.cxx的第28行反序列化输入档案时,在下面的第一个代码段中显示了一个异常(请参见下面的第5个代码段,第28行已标记)。
Is there something I am missing in this de-serialization procedure implemented in TestBE.cxx in code snippet 5? 在代码片段5的TestBE.cxx中实现的反序列化过程中,我缺少什么吗? Why do you think I am getting this exception? 为什么您认为我遇到这种例外情况? Could it be that I am missing st in the serialization procedure implemented in ZMQHandler.cxx (code snippet 3 - function sendToBE). 可能是我在ZMQHandler.cxx(代码片段3-sendToBE函数)中实现的序列化过程中缺少了st。 Thanks in advance. 提前致谢。
CODE SNIPPET 1 GDB OUTPUT & backtrace 代码片段1 GDB输出和回溯
$ gdb TestBE
GNU gdb (GDB) 7.5-ubuntu
.....
(gdb) r
Starting program: /TestBE
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
[New Thread 0xb7c12b40 (LWP 16644)]
[New Thread 0xb7411b40 (LWP 16645)]
Connecting to FE...
!!!!!!!!!!!!!!!!!!!!HERE BEGINS!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!! HERE BEINSS !!!!!!!!!!!!!!!!!!!!!!!!!!!
**CHAR [22 serialization::archive 9 0 1 0
0 1 12 Hello there!]
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid**
!!!!!!!!!!!!!!!!!!!!HERE END !!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!! HERE END !!!!!!!!!!!!!!!!!!!!!!!!!!!
Program received signal SIGABRT, Aborted.
0xb7fdd424 in __kernel_vsyscall ()
(gdb) bt
#0 0xb7fdd424 in __kernel_vsyscall ()
#1 0xb7c7a1df in raise () from /lib/i386-linux-gnu/libc.so.6
#2 0xb7c7d825 in abort () from /lib/i386-linux-gnu/libc.so.6
#3 0xb7e608ad in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/i386- linux-gnu/libstdc++.so.6
#4 0xb7e5e4f3 in ?? () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#5 0xb7e5e52f in std::terminate() () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#6 0xb7e5e825 in __cxa_rethrow () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#7 0x0804c1d4 in boost::archive::detail::pointer_iserializer<boost::archive::text_iarchive, GenericMessage<std::string> >::load_object_ptr (this=0x8054444, ar=...,
x=@0x805cb40: 0x805cb50, file_version=0) at /usr/include/boost/archive/detail/iserializer.hpp:327
#8 0xb7f3839d in boost::archive::detail::basic_iarchive::load_pointer(void*&, boost::archive::detail::basic_pointer_iserializer const*, boost::archive::detail::basic_pointer_iserializer const* (*) (boost::serialization::extended_type_info const&)) () from /usr/lib/libboost_serialization.so.1.49.0
#9 0x0804bea9 in boost::archive::detail::load_pointer_type<boost::archive::text_iarchive>::invoke<GenericMes sage<std::string>*> (ar=..., t=@0xbfffef70: 0xbfffeff8)
at /usr/include/boost/archive/detail/iserializer.hpp:524
#10 0x0804be55 in boost::archive::load<boost::archive::text_iarchive, GenericMessage<std::string>*> (ar=..., t=@0xbfffef70: 0xbfffeff8)
at /usr/include/boost/archive/detail/iserializer.hpp:592
#11 0x0804be36 in boost::archive::detail::common_iarchive<boost::archive::text_iarchive>::load_override<Gener icMessage<std::string>*> (this=0xbfffef84, t=@0xbfffef70: 0xbfffeff8)
at /usr/include/boost/archive/detail/common_iarchive.hpp:66
#12 0x0804be14 in boost::archive::basic_text_iarchive<boost::archive::text_iarchive>::load_override<GenericMe ssage<std::string>*> (this=0xbfffef84, t=@0xbfffef70: 0xbfffeff8)
at /usr/include/boost/archive/basic_text_iarchive.hpp:65
#13 0x0804bdf2 in boost::archive::text_iarchive_impl<boost::archive::text_iarchive>::load_override<GenericMes sage<std::string>*> (this=0xbfffef84, t=@0xbfffef70: 0xbfffeff8)
at /usr/include/boost/archive/text_iarchive.hpp:82
#14 0x0804bcec in boost::archive::detail::interface_iarchive<boost::archive::text_iarchive>::operator>> <GenericMessage<std::string>*> (this=0xbfffef84, t=@0xbfffef70: 0xbfffeff8)
at /usr/include/boost/archive/detail/interface_iarchive.hpp:60
#15 0x0804b2a1 in main () at TestBE.cxx:28
CODE SNIPPET 2 (GenericMessage.hxx) 代码片段2(GenericMessage.hxx)
#include <iostream>
#include <string>
#include <sstream>
#include <boost/serialization/serialization.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
template <class T>
class GenericMessage {
public:
GenericMessage():
beId(-1),
data(NULL)
{}
GenericMessage(int id, T msg):
beId(id),
data(msg)
{}
~GenericMessage(){}
T getData()
{
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:
friend class boost::serialization::access;
int beId;
T data;
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & beId;
ar & data;
}
};
CODE SNIPPET 3 (ZmqHandler.hxx) 代码片段3(ZmqHandler.hxx)
#include "zmq.hpp"
#include "GenericMessage.hxx"
#include <unistd.h>
#include <cassert>
template <class A>
class ZmqHandler {
public:
ZmqHandler():
mContext(1),
mOutbHandlerSocket(mContext, ZMQ_PUSH)
{
mOutbHandlerSocket.bind ("tcp://*:5555");
}
~ZmqHandler() {}
void sendToBE(GenericMessage<A> *theMsg)
{
std::ostringstream archive_stream;
boost::archive::text_oarchive archive(archive_stream);
try
{
archive << theMsg;
} catch (boost::archive::archive_exception& ex) {
std::cout << "Archive Exception during deserializing:" << std::endl;
std::cout << ex.what() << std::endl;
} catch (int e) {
std::cout << "EXCEPTION " << e << std::endl;
}
std::string outbound_data_ = archive_stream.str();
const char * buf = outbound_data_.c_str();
int len = strlen((const char*)buf);
std::cout << "LENGTH [" << len << "]" << std::endl;
zmq::message_t msgToSend(len);
memcpy ((char *) msgToSend.data(), buf, len);
if(memcmp((char *) msgToSend.data(), buf, len) != 0)
{
std::cout << "memcpy error!" << std::endl;
}
mOutbHandlerSocket.send(msgToSend);
std::cout << "SENT request: [" << theMsg->toString() << "]" << std::endl;
}
private:
zmq::context_t mContext;
zmq::socket_t mOutbHandlerSocket;
};
CODE SNIPPET 4 (TestFE.cxx) 代码片段4(TestFE.cxx)
#include "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 5 (TestBE.cxx) 代码片段5(TestBE.cxx)
#include "zmq.hpp"
#include "GenericMessage.hxx"
#include <fstream>
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);
const char *buf = static_cast<const char*>(reply.data());
std::cout << "CHAR [" << buf << "]" << std::endl;
std::string input_data_(buf);
std::istringstream archive_stream(input_data_);
boost::archive::text_iarchive archive(archive_stream);
GenericMessage<std::string> *theMsg;
try
{
/* !!!!!!!!!! LINE 28 is the following !!!!!!!!!!*/
archive >> theMsg;
} catch (boost::archive::archive_exception& ex) {
std::cout << "Archive Exception during deserializing:" << std::endl;
std::cout << ex.what() << std::endl;
} catch (int e) {
std::cout << "EXCEPTION " << e << std::endl;
}
std::cout << "ID" << theMsg->getBeId() << std::endl;
std::cout << "Data" << theMsg->getData() << std::endl;
}
return 0;
}
You declared theMsg
as a pointer ( GenericMessage<std::string> *theMsg;
). 您将theMsg
声明为指针( GenericMessage<std::string> *theMsg;
)。
Try changing that line to GenericMessage<std::string> theMsg;
尝试将该行更改为GenericMessage<std::string> theMsg;
.` 。
The real source of your exception 异常的真正根源
In the GenericMessage
default constructor, you initialize data
with NULL
. 在GenericMessage
默认构造函数中,您data
使用NULL
初始化data
。 However, you aren't allowed to initialize a std::string
with a NULL
pointer. 但是,不允许您使用NULL
指针初始化std::string
。 Don't initialize your data
member in the default constructor. 不要在默认构造函数中初始化data
成员。
GenericMessage()
: beId(-1)
{}
As long as your type T
has a default constructor, the compiler will handle its initialization when the template is generated. 只要您的T
类型具有默认构造函数,则在生成模板时,编译器将处理其初始化。
(hopefully) helpful hint #1 (希望)有用的提示1
The data buffer in a zmq::message_t
is (generally) not NULL-terminated. zmq::message_t
的数据缓冲区(通常)不是NULL终止的。 After you receive the message, be careful about how you convert the buffer to a string. 收到消息后,请注意如何将缓冲区转换为字符串。
// snip
zmq::message_t reply;
socket.recv (&reply);
const char *buf = static_cast<const char*>(reply.data());
std::cout << "CHAR [" << buf << "]" << std::endl;
//std::string input_data_(buf); // assumes a null-term string
std::string input_data_( buf, reply.size() );
// snip
(hopefully) helpful hint #2 (希望)有用的提示2
Also, I noticed something in ZmqHandler.hxx . 另外,我在ZmqHandler.hxx中注意到了一些东西。
// snip
std::string outbound_data_ = archive_stream.str();
const char * buf = outbound_data_.c_str();
int len = strlen((const char*)buf);
std::cout << "LENGTH [" << len << "]" << std::endl;
zmq::message_t msgToSend(len);
memcpy ((char *) msgToSend.data(), buf, len);
if(memcmp((char *) msgToSend.data(), buf, len) != 0)
{
std::cout << "memcpy error!" << std::endl;
}
// snip
You don't need to check the results of the memcpy (unless you want to check its return value). 您不需要检查memcpy的结果(除非您想检查其返回值)。 The whole block could be changed to something like this: 整个块可以更改为以下形式:
std::string outbound_data_ = archive_stream.str();
// no need to use the c-style string function 'strlen'
int len = outbound_data_.length();
std::cout << "LENGTH [" << len << "]" << std::endl;
zmq::message_t msgToSend(len);
memcpy( msgToSend.data(), outbound_data_.data(), len );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.