Increase async_write problem

I will show a piece of code;

void wh(const boost::system::error_code& ec,
        std::size_t bytes_transferred)
{
    std::cout << "test";
}

int main(int argc, char* argv[]) 
{ 
    boost::asio::io_service pService;
    boost::asio::serial_port pSerial(pService,"COM4");

    while (true) {
        boost::asio::async_write(pSerial, boost::asio::buffer("A",1),&wh);
    }

    return 0; 
} 

when I use this code, I get a memory leak, I found a piece of code such as a minicom_client tutorial, even the complex one from this code, also I get a memory leak on minicom_client. If i use

    boost::asio::write(pSerial, boost::asio::buffer("A",1));

instead of async_write it works well, could you explain what is going on there, thanks a lot ...

+3
source share
1 answer

You are not using async_writecorrectly. This is a complex operation, and the application is not required to responsibility that no other calls async_writeon pSerialnot performed as long as recording is called the handler. the documentation perfectly describes this

async_write_some, . The , (, async_write, async_write_some , ) .

. , async_write wh(). io_service::run() . , examples .

int main(int argc, char* argv[]) 
{ 
    boost::asio::io_service pService;
    boost::asio::serial_port pSerial(pService,"COM4");

    boost::asio::async_write(
        pSerial,
        boost::asio::buffer("A",1),
        boost::bind(
            &wh,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred
       );

    pService.run();

    return 0; 
} 

, , . , boost::shared_ptr async_write boost::bind. asio.

+7

Source: https://habr.com/ru/post/1792028/


All Articles