升压async_write问题 [英] Boost async_write problem

查看:169
本文介绍了升压async_write问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会表现出一定的一块code的;

i'll show some 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; 
} 

当我使用code我得到的内存泄漏,我发现了一些片code像minicom_client教程从code还我越来越对minicom_client内存泄漏即使是复杂的。如果我使用

when i use that code i'm getting memory leak, I found some piece of code like minicom_client tutorial even complex from that code also i'm getting memory leak on minicom_client. If i use

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

而不是async_write它工作得很好,你能解释一下这是怎么回事那里,非常感谢......

instead of async_write it works well, Could you explain what's going on there , Thanks a lot ...

推荐答案

您在不使用 async_write 正确。这是一个沉稳的操作,而这取决于应用程序的责任,以确保在 pSerial 没有其他呼叫 async_write 制成直到写处理函数。在<一个href=\"http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/reference/async_write/overload1.html\">documentation总结了很好

You're not using async_write correctly. It is a composed operation, and it's up to the application's responsibility to ensure no other calls to async_write on pSerial are made until the write handler is invoked. The documentation summarizes this nicely

此操作在执行方面
  零个或多个呼叫流的
  async_write_some功能,并且是
  称为组成的操作。
  程序必须确保该流
  不执行其他的写操作
  (如async_write,流的
  async_write_some功能,或者任何
  执行其他操作组成的
  写),直至该操作
  完成。

重点由我补充道。要解决你的应用程序,你需要启动其他 async_write
WH()方法中的操作。您还需要调用 :: io_service对象的run()来的揭开序幕异步事件循环。如果这个概念你不熟悉的,我建议学习之前href=\"http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/examples.html\">范例的

emphasis added by me. To fix your application, you'll need to start additional async_write operations from within your wh() method. You'll also need to invoke io_service::run() to kick off the asynchronous event loop. If this concept is unfamiliar to you, I suggest studying the examples prior to writing your own code.

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; 
} 

这也是值得一提的code作为写是危险的,因为缓冲区会走出范围内调用处理程序之前。这通常是由保留缓冲区传递一个的boost :: shared_ptr的 async_write 的对象的成员来完成通过的boost ::绑定处理程序。这个概念是在ASIO例子prevalent。

It's also worth noting your code as written is dangerous since the buffer will go out of scope before the handler is invoked. Typically this is accomplished by retaining the buffer as a member of an object that passes a boost::shared_ptr to the async_write handler via boost::bind. This concept is prevalent in the asio examples.

这篇关于升压async_write问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆