c++ - Crash when using boost serialize a lot of times -
i making program serialize struct of data , send server. problems come when function serialize fails when has been use, more or less 40 times or so.
i using visual studio community 2015, boost 1.59 32bits , winsock2, program compiled 32 bits architecture.
the error is:
exception produced in 0x772be3c6 (ntdll.dll) in test.exe: 0xc0000005: access violation reading location 0x3838e1a9.
and here simple example using function crash:
#define win32_lean_and_mean #include <winsock2.h> #include <ws2tcpip.h> #include <boost/archive/binary_oarchive.hpp> #include <boost/archive/binary_iarchive.hpp> #include <boost/iostreams/device/back_inserter.hpp> #include <boost/iostreams/device/array.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/iostreams/stream.hpp> struct structfunction { char function; int arguments; template<class archive> void serialize(archive &ar, const unsigned int version) { ar & function; ar & arguments; } }; boost_class_implementation(structfunction, boost::serialization::object_serializable) bool send(std::string buffer) { int iresult = send(connectsocket, buffer.data(), buffer.size(), 0); if(iresult == socket_error) { printf("send failed: %d\n", wsagetlasterror()); stop(); return false; } return true; } int serializeandsend(structfunction structfunction) { // serialize obj std::string std::string serial_str; serial_str.clear(); boost::iostreams::back_insert_device<std::string> inserter(serial_str); boost::iostreams::stream<boost::iostreams::back_insert_device<std::string> > s(inserter); boost::archive::binary_oarchive oa(s); // throws error oa << structfunction; // flush stream finish writing buffer s.flush(); send(serial_str); return 1; } int main(int argc, char* argv[]) { connectsocket = /* create socket connected server using winsock2 */ structfunction structfunction; structfunction.function = "a"; structfunction.parameter = 1; serializeandsend(structfunction); }
in real code function serializeandsend called 40 times aprox.
i can say, 90% sure, struct serialize initialize , not have wrong value.
i have try clean projects in client , server. ram low, 13mb more or less.
i not know why fails when has been use , not in first time.
the idea of eliminating causes, make thing self-contained , observe whether still reproduces the/a problem:
#include <boost/archive/binary_oarchive.hpp> #include <boost/archive/binary_iarchive.hpp> #include <boost/iostreams/device/back_inserter.hpp> #include <boost/iostreams/device/array.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/iostreams/stream.hpp> struct structfunction { char function; int arguments; template<class archive> void serialize(archive &ar, const unsigned int version) { ar & function; ar & arguments; } }; boost_class_implementation(structfunction, boost::serialization::object_serializable) bool send(std::string buffer) { /* * int iresult = send(connectsocket, buffer.data(), buffer.size(), 0); * * if(iresult == socket_error) * { * printf("send failed: %d\n", wsagetlasterror()); * stop(); * return false; * } */ return true; } int serializeandsend(structfunction structfunction) { // serialize obj std::string std::string serial_str; namespace bio = boost::iostreams; { bio::stream<bio::back_insert_device<std::string> > s(serial_str); boost::archive::binary_oarchive oa(s); oa << structfunction; } static bool do_init = true; if (do_init) { std::cout << "debug: \n"; (uint8_t ch : serial_str) std::cout << std::setw(2) << std::setfill('0') << std::showbase << std::hex << static_cast<int>(ch) << " "; do_init = false; } send(serial_str); return 1; } int main() { //connectsocket = [> create socket connected server using winsock2 <] (auto = 0ull; i< 1ull << 22; ++i) { structfunction structfunction; structfunction.function = 'a'; structfunction.arguments = 1; serializeandsend(structfunction); } }
as can see, not lot of trouble, output:
debug: 0x16 00 00 00 00 00 00 00 0x73 0x65 0x72 0x69 0x61 0x6c 0x69 0x7a 0x61 0x74 0x69 0x6f 0x6e 0x3a 0x3a 0x61 0x72 0x63 0x68 0x69 0x76 0x65 0xd 00 0x4 0x8 0x4 0x8 0x1 00 00 00 0x41 0x1 00 00 00 real 0m24.026s user 0m24.010s sys 0m0.000s
what next?
try on your system. if it's ok, problem in send
(and related functions).
if see problem on platform, consider (memory) profiling , enabling debug heap diagnostics.
Comments
Post a Comment