The idea is this: there is a thread that checks in turn whether something came to the sockets ( vecSock ) from a specific address ( vecEndpoint ), writes it to the buffers ( vecBuf ), another thread processes these buffers. Buffers are implemented on the basis of boost::circular_buffer with mutexes when modifying the contents. As a result, I found out that the problem is in vecSock[i]->async_receive_from , since if this line is replaced with vecSock[i]->receive_from(boost::asio::buffer(data_.data, 8000), vecEndpoint[i]); followed by similar ( async_receive_from lambda function) data processing, everything works fine, except that it is not what is required.
I io_service problem is io_service .
//объявления в членах класса: typedef std::shared_ptr<udp::socket> SHP_Socket; boost::asio::io_service io_service_; vector<SHP_Socket> vecSock; vector<boost::shared_ptr<boost::thread>> receive_threads; vector<udp::endpoint> vecEndpoint; vector<CThreadedCircular> vecBuf; bool process_all_finishing=false; //реализация методов: void CRTPReceive::Run() { io_service_.run(); } void CRTPReceive::reinit_sockets(bool mode) { int size = net_.my_ports.size(); vecSock.resize(size); vecEndpoint.resize(size); for (int i = 0; i < size; ++i) { vecSock[i].reset(new udp::socket(io_service_, udp::endpoint(udp::v4(), net_.my_ports[i]))); } } int CRTPReceive::process_all() { boost::shared_ptr<boost::thread> thread1(new boost::thread(&CRTPReceive::receive, this)); receive_threads.push_back(thread1); std::thread a(&CRTPReceive::Run, this); return 0; } void CRTPReceive::receive() { while (process_all_finishing == false) { try { for (unsigned i = 0; i < vecSock.size(); ++i) { Data data_; vecSock[i]->async_receive_from( boost::asio::buffer(data_.data, 8000), vecEndpoint[i],// [this, data_, i](boost::system::error_code ec, std::size_t szPack) { if (szPack > 12) { char DATA[szPack-12]; memcpy(DATA, data_.data + 12, szPack - 12); vecBuf[i].push(DATA); } }); } } catch (std::exception& e) { cout<<"\nExeption"; } } } Getting out errors:
First-chance exception at 0x771FF70B (ntdll.dll) in server.exe: 0xC0000374: The heap was damaged (parameters: 0x77234270). Unhandled exception at 0x771FF70B (ntdll.dll) in server.exe: 0xC0000374: The heap was damaged (parameters: 0x77234270).
First-chance exception at 0x7715E546 (ntdll.dll) in server.exe: 0xC0000005: Access violation reading location 0xE768E351.
Unhandled exception at 0x7715E546 (ntdll.dll) in server.exe: 0xC0000005: Access violation reading location 0xE768E351.
First-chance exception at 0x608A70D1 (msvcr120.dll) in server.exe: 0xC0000005: Access violation reading location 0xFEEEFF32.
UPDATE1: from the answer Pavel Parshin obtained the following result: 1) Added a reception buffer to the properties of the class, so its life cycle is comparable with the class. 2) removed all unnecessary for verification from the receive method:
void CRTPReceive::receive() { while (process_all_finishing == false) { try { for (unsigned i = 0; i < vecSock.size(); ++i) { vecSock[i]->async_receive_from( boost::asio::buffer(vecData[i].data, 8000), vecEndpoint[i],// [this](boost::system::error_code ec, std::size_t szPack) { cout << "\nw"; if (szPack > 12) { cout << "\nqqqqq"; } }); } } catch (std::exception& e) { ; } } } Nothing is displayed on the screen. Why?