如何避免内存泄漏?

时间:2014-02-10 00:54:04

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

我有以下代码。我无法确定内存泄漏,我认为它与正确处理流有关。我在整个程序中使用相同的代码,所以一切都被妥善处理,因为它只会变得更糟。

boost::system::error_code ec;

    using boost::asio::ip::tcp;
    namespace ssl = boost::asio::ssl;
    typedef ssl::stream<tcp::socket> ssl_socket;

    boost::asio::io_service io_service;

    ssl_socket sockconn(io_service, ctxPull);

    tcp::resolver resolver(io_service);

    tcp::resolver::query query("testserver.com", "https"); //120ms

    sockconn.lowest_layer().open(boost::asio::ip::tcp::v4());
    sockconn.lowest_layer().bind(
            boost::asio::ip::tcp::endpoint(
                    boost::asio::ip::address::from_string(strBoundIP), 0));

    sockconn.lowest_layer().set_option(tcp::no_delay(true));

    boost::asio::connect(sockconn.lowest_layer(), resolver.resolve(query));

    sockconn.lowest_layer().set_option(tcp::no_delay(true));

    sockconn.set_verify_mode(boost::asio::ssl::verify_none);
    sockconn.handshake(ssl_socket::client);

    boost::asio::streambuf request;
    std::ostream requestStream(&request);




        requestStream << "GET / HTTP/1.1\r\n"
                    << "Accept-Language: en-us\r\n"
                    << "Host: testserver.com\r\n"
                    << "User-Agent: TestClient 1.1\r\n"
                    << "Connection: Close\r\n\r\n";


    boost::asio::write(sockconn, request);
    boost::asio::streambuf respond;

    boost::asio::read(sockconn, respond, completion, ec);

    cout << ec.message() << "\n";

    std::string strOutput;

    strOutput = boost::asio::buffer_cast<const char*>(respond.data());

    respond.consume(strOutput.size());

    cout << "OUTPUT: " << strOutput << "\n";

0 个答案:

没有答案