I have 2 different applications, a sender and a receiver. the Sender will send a message over to the receiver, who will decode the message and print to the console. However, I keep getting segmentation fault error.
Both sender and receiver application have the same TestContainer.h and TestContainer.cpp.
Casting method
template<class To,class From>To cast(From v)
{
return static_cast<To>(static_cast<const void*>)(v);
}
Sender application
int main()
{
TestContainer tc;
tc.setDesc("this is a message");
const char* castedData = cast<const char*>(&tc);
const TestContainer test_tc = cast<const TestContainer*>(castedData);
// i get back "this is a message",so the casting is working
cout << "message content: " << test_tc->getDesc() <<endl;
TaoSender;
TaoSender.send(castedData);
return 1;
}
Receiver Application
void push(const RtecEventComm::EventSet& events)
{
const char* receivedData;
events[0].data.any_value >>= receivedData;
cout << "data received: " << receivedData << endl;
const TestContainer rcv_tc = cast<const TestContainer*>(receivedData);
cout << "message content: " << rcv_tc->getDesc() <<endl; // error(segmentation fault)
}
TestContainer.h and TestContainer.cpp
class TestContainer{
public
TestContainer();
virtual ~TestContainer();
const std:string& getDesc () const {
return desc;
}
void setDesc(const std::string& desc) {
this->desc = desc;
}
private
std::string desc;
}
#include TestContainer.h
TestContainer::TestContainer(){}
TestContainer::~TestContainer(){}
The value of castedData at the sender and the value of receivedData at the receiver is the same, so I guess the message sending is correct.
However,at the Receiver, after converting the receivedData buffer to a Testcontainer pointer and attempting to access the desc, I get a segmentation fault error.
I also tried casting back to Testcontainer in the Sender, and I am able to access the desc. So what did I miss out?