使用BOOST的asio库连接服务器发生内存泄露问题,附代码~~~

blldw 2012-08-04 01:25:58
以下代码可在vs2008下编译通过,其中61.135.169.125为百度网站的IP地址,作为测试用。程序产生的问题在于,当用任务管理器查看程序耗占的内存时,发现耗占内存以8K,12k或者更大的数值持续增加。如果注释掉connect方法的调用,则没有内存泄露问题。因此应该Net_connector的connect方法中。但是该方法以及其调用的其它方法都没有内存分配的操作,不知道是否是asio库的使用存在什么问题。请大家提出看法,谢谢~~~~

Net_connector.h
#ifndef _NET_CONNECTOR_H
#define _NET_CONNECTOR_H

#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <memory>
#include <string>
#include <boost/date_time.hpp>

/**
* Net_connector is a class implementing asynchronous connection to remote
* location.
*/
class Net_connector {
public:
/**
* Construct Net_connector object with io_service object.
*
* @param[in] io_service boost::asio::io_service object for deadline_time
*/
Net_connector(boost::asio::io_service& io_service,
boost::asio::ip::tcp::socket& socket);

/**
* connect to specified remote location within a period time.
*
* @param[in] ip the ip address to connect
* @param[in] port the port on remote location to connect
* @param[in] seconds a period time in second
*
* @return true if successfully made connection to remote location
*/
bool connect(const std::string& ip, const unsigned short& port,
const boost::posix_time::seconds& seconds);

/** return true if the connection is established, or false. */
operator bool() const;

private:
/** asynchronous connection handler. */
void handle_connect(const boost::system::error_code& error);

/** asynchronous wait handler. */
void handle_timeout(const boost::system::error_code& error);

/** reference socket for asynchronous connection. */
boost::asio::ip::tcp::socket& _socket_ref;

/** deadline_timer object for implementing asynchronous connection. */
boost::asio::deadline_timer _dealine_timer;

/** time point to computing time period for specified operation. */
//std::chrono::system_clock::time_point _last_time_point;

/** flag indicates whether or not connection is made with a period time. */
bool _connected;

/** mechanism for implementing asynchronous connection. */
boost::mutex _mutex;
boost::condition_variable _cond_var;
};

#endif // _NET_CONNECTOR_


Net_connector.cpp
#include "Net_connector.h"
#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>

Net_connector::Net_connector(boost::asio::io_service& io_service,
boost::asio::ip::tcp::socket& socket)
: _socket_ref(socket)
, _dealine_timer(io_service)
, _connected(false)
{
}

bool Net_connector::connect(const std::string& ip, const unsigned short& port,
const boost::posix_time::seconds& seconds)
{
boost::mutex::scoped_lock lock(_mutex);

boost::asio::ip::tcp::endpoint endpoint(
boost::asio::ip::address::from_string(ip), port);

_connected = false;

_dealine_timer.expires_from_now(seconds);
_dealine_timer.async_wait(boost::bind(&Net_connector::handle_timeout,
this, boost::asio::placeholders::error));

_socket_ref.async_connect(endpoint,
boost::bind(&Net_connector::handle_connect, this,
boost::asio::placeholders::error));

_cond_var.wait(lock);

return _connected;
}

void Net_connector::handle_connect(const boost::system::error_code& error)
{
if (boost::asio::error::operation_aborted == error) {
return;
}

if (!error) {
_connected = true;
}

_dealine_timer.cancel();
}

void Net_connector::handle_timeout(const boost::system::error_code& error)
{
boost::mutex::scoped_lock lock(_mutex);

// timeout, cancel asynchronous connect operation
if (boost::asio::error::operation_aborted != error) {
boost::system::error_code ec;
_socket_ref.close(ec);
}

_cond_var.notify_one();
}

Net_connector::operator bool() const
{
return _connected;
}


main.cpp
#include "Net_connector.h"
#include <boost/thread.hpp>

void run_service(boost::asio::io_service& io_service)
{
io_service.run();
}

int main(int, char**)
{
boost::asio::io_service io_service;
boost::asio::io_service::work work(io_service);
boost::thread t(run_service, boost::ref(io_service));

while (true) {
boost::asio::ip::tcp::socket socket(io_service);
boost::system::error_code ec;

Net_connector connector(io_service, socket);
if (!connector.connect("61.135.169.125", 80, boost::posix_time::seconds(5))) {
std::cout << "failed to connect server" << std::endl;
}

socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
socket.close(ec);
}

return 0;
}

...全文
294 点赞 收藏 2
写回复
2 条回复
hurryboylqs 2012年08月07日
你用的死循环,机器不卡死?
回复 点赞
发动态
发帖子
工具平台和程序库
创建于2007-09-28

8235

社区成员

2.7w+

社区内容

C/C++ 工具平台和程序库
社区公告
暂无公告