我的团队现在已经有几个星期这个问题了,我们有点难过.善意和知识将优雅地收到!
使用嵌入式系统,我们试图序列化一个对象,通过Linux套接字发送它,在另一个进程中接收它,然后将它反序列化回原始对象.我们有以下反序列化功能:
/*! Takes a byte array and populates the object's data members */
std::shared_ptr<Foo> Foo::unmarshal(uint8_t *serialized, uint32_t size)
{
auto msg = reinterpret_cast<Foo *>(serialized);
return std::shared_ptr<ChildOfFoo>(
reinterpret_cast<ChildOfFoo *>(serialized));
}
Run Code Online (Sandbox Code Playgroud)
该对象已成功反序列化,可以从中读取.但是,当std::shared_ptr<Foo>调用返回的析构函数时,程序会出现段错误.Valgrind给出以下输出:
==1664== Process terminating with default action of signal 11 (SIGSEGV)
==1664== Bad permissions for mapped region at address 0xFFFF603800003C88
==1664== at 0xFFFF603800003C88: ???
==1664== by 0x42C7C3: std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() (shared_ptr_base.h:149)
==1664== by 0x42BC00: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() (shared_ptr_base.h:666)
==1664== by 0x435999: std::__shared_ptr<ChildOfFoo, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() (shared_ptr_base.h:914)
==1664== by 0x4359B3: std::shared_ptr<ChildOfFoo>::~shared_ptr() (shared_ptr.h:93)
Run Code Online (Sandbox Code Playgroud)
我们对任何建议都持开放态度!感谢您的时间 :)