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?

    1 answer 1

    From the documentation for async_receive_from :

    Although it is not necessary to take into account, it’s not clear that it’s not clear that it’s not the case.

    That is, the user is responsible for the life cycle of the buffer, and the buffer must be valid until the handler .

    In your case, the data_ variable is deleted after calling async_receive_from , since the lambda function does not extend the life cycle of the captured variables + you capture by value (i.e. by copying), which is also logically incorrect. I think, from here and problems with memory.

    About the fact that logging is not happening: I think the problem is in io_service_.run(); - in fact, it is a blocking function that runs the event loop and processes all requests. In your case, this function completes instantly and handlers are not executed. Try this:

      try { for (unsigned i = 0; i < vecSock.size(); ++i) { vecSock[i]->async_receive_from(...); } Run(); } catch (std::exception& e) { loggit("Exeption: " + boost::to_string(e.what()),0); } 
    • Thanks, made buffer in class properties. More precisely, the vector of the buffers (for now let it be so). But now some other problem. Lambda function is not executed. Now I’ll add an update to the question header - Dmitry
    • @Dmitry, updated the answer - Pavel Parshin
    • And now it is very strange: I send 172 bytes, but 2048 is received ... Why is that? At the same time, the message is exactly what I am sending, but the szPack variable takes the value 2048, which is extremely szPack . By the way, the debug print with cout works in the lambda function - Dmitry