使用io_service :: run时内存泄漏

时间:2016-08-30 06:56:25

标签: c++ boost memory-leaks boost-asio

我正在尝试编写我的套接字类(下面的代码)。 最后一切正常:重新连接,连接,消息发送,消息接收

但是当我重复TCPSocketBody :: Connect(const std :: string& adress,const std :: string& port)时,如果例如主机无​​法访问,我注意到内存泄漏。我的应用程序的大小正在增长。

我发现当我删除行时:

thread_io_service = boost::thread(boost::bind(&boost::asio::io_service::run, &io_service_global));
问题消失了。但我需要这条线。我做错了什么? 我从应用程序中的命令行号检查了它,它总是1或2.

#include "TCPSocketBody.h"

TCPSocketBody::TCPSocketBody() : socket_(io_service_global),
                                 resolver(io_service_global),
                                 connected(false),
                                 expectedMessage(0)
{

}

void TCPSocketBody::Close()
{
  io_service_global.post(boost::bind(&TCPSocketBody::DoClose, this));
}

void TCPSocketBody::Connect(const std::string &adress, const std::string &port)
{
    io_service_global.reset();
    iterator = resolver.resolve({adress, port});

     boost::asio::async_connect(socket_, iterator,
           boost::bind(&TCPSocketBody::HandleConnect, this,
           boost::asio::placeholders::error));


     socket_.set_option(boost::asio::ip::tcp::no_delay(true));

     thread_io_service = boost::thread(boost::bind(&boost::asio::io_service::run, &io_service_global));

}

void TCPSocketBody::HandleConnect(const boost::system::error_code& error)
{
  if (!error)
  {           
      connected = true;

      boost::asio::async_read(socket_,
         boost::asio::buffer(data_to_read, MessageCoder::BufferSize()), boost::asio::transfer_all(),
         boost::bind(&TCPSocketBody::HandleReadHeader, this,
           boost::asio::placeholders::error));

  } else
  {
        Close();
  }
}

void TCPSocketBody::HandleReadHeader(const boost::system::error_code& error)
{
    expectedMessage = MessageCoder::HeaderToVal(data_to_read);

    if (expectedMessage > MessageCoder::MaxMessageSize())
    {
       expectedMessage = 0;
       Close();
    } else
    {
        boost::asio::async_read(socket_,
           boost::asio::buffer(data_to_read, expectedMessage), boost::asio::transfer_all(),
           boost::bind(&TCPSocketBody::HandleReadMessage, this,
             boost::asio::placeholders::error));
    }
}

void TCPSocketBody::HandleReadMessage(const boost::system::error_code& error)
{
    expectedMessage = 0;        

    boost::asio::async_read(socket_,
       boost::asio::buffer(data_to_read, MessageCoder::BufferSize()), boost::asio::transfer_all(),
       boost::bind(&TCPSocketBody::HandleReadHeader, this,
         boost::asio::placeholders::error));
}

void TCPSocketBody::WriteMessage(char *dataToSend)
{
    io_service_global.post(boost::bind(&TCPSocketBody::Write, this, dataToSend));
}

void TCPSocketBody::Write(char *dataToSend)
{
    data = dataToSend;

    boost::asio::async_write(socket_,
        boost::asio::buffer(data, std::strlen(data)),
        boost::bind(&TCPSocketBody::HandleWrite, this,
          boost::asio::placeholders::error));

}

void TCPSocketBody::HandleWrite(const boost::system::error_code& error)
{
  if (!error)
  {

  }
  else
  {
        Close();
  }
}

void TCPSocketBody::DoClose()
{
  socket_.close();
  connected = false;
}

1 个答案:

答案 0 :(得分:0)

没有头文件,很难确定。我怀疑thread_io_service是一个成员变量。第二次打电话给' Connect'将在不破坏对象的情况下覆盖现有值。在对象内分配的任何内存都将泄露。

也许尝试在connect调用中创建一个std :: vector of thread_io_service和push_back每个新线程。向量将在被破坏时销毁所包含的对象。

Valgrind可能会更详细地指出这个问题。