OpenSSL CRYPTO_malloc泄漏,我该如何解冻?

时间:2015-09-24 20:14:38

标签: c++ c ssl memory-leaks

我有一个非常简单的应用程序,它通过一个主机名列表并连接到HTTPS端口上的每个主机名,以获取客户端识别数据的新服务器数据。

为了获取数据我使用的是OpenSSL,但似乎每次都在泄漏内存。

负责连接/放置/接收SSL数据的类。

class CConnector
{
public:

    static std::string GetData (const std::string& strHostName)
    {
        // Initialize malloc, free, etc for OpenSSL's use
        CRYPTO_malloc_init();

        // Initialize OpenSSL's SSL libraries
        SSL_library_init();

        // Load all available encryption algorithms
        OpenSSL_add_all_algorithms();

        //
        std::string strRequest="GET /\r\n";

        // Set up a SSL_CTX object, which will tell our BIO object how to do its work
        SSL_CTX* ctx = SSL_CTX_new(SSLv23_client_method());

        // Create our BIO object for SSL connections.
        BIO* bio = BIO_new_ssl_connect(ctx);

        // Create a SSL object pointer, which our BIO object will provide.
        SSL* ssl = NULL;

        // Failure?
        if (bio == NULL)
        {
            CLogger::Instance()->Write(XLOGEVENT_LOCATION,CLogger::eState::ERROR, "BIO");
            ERR_print_errors_fp(stderr);

            if(ctx!=NULL)SSL_CTX_free(ctx);
            if(bio!=NULL)BIO_free_all(bio);

            return "";
        }

        // Makes ssl point to bio's SSL object.
        BIO_get_ssl(bio, &ssl);

        // Set the SSL to automatically retry on failure.
        SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);

        // We're connection to google.com on port 443.
        std::string strHost = GetHostFromURL(strHostName);
        strHost+=":https";

        //
        BIO_set_conn_hostname(bio, strHost.data());

        // Same as before, try to connect.
        if (BIO_do_connect(bio) <= 0)
        {
            CLogger::Instance()->Write(XLOGEVENT_LOCATION,CLogger::eState::ERROR, "cannot connect");

            if(ctx!=NULL)SSL_CTX_free(ctx);
            if(bio!=NULL)BIO_free_all(bio);

            return "";
        }

        // Now we need to do the SSL handshake, so we can communicate.
        if (BIO_do_handshake(bio) <= 0)
        {
            CLogger::Instance()->Write(XLOGEVENT_LOCATION,CLogger::eState::ERROR, "SSL Handshake");

            if(ctx!=NULL)SSL_CTX_free(ctx);
            if(bio!=NULL)BIO_free_all(bio);

            return "";
        }

        // Create a buffer for grabbing information from the page.
        char buf[1024];
        memset(buf, 0, sizeof(buf));

        // BIO_puts sends a null-terminated string to the server.
        BIO_puts(bio, strRequest.c_str());

        int iChars = 0;
        while (1)
        {
            iChars = BIO_read(bio, buf, sizeof(buf)-1);

            // Close reading
            if (iChars <= 0)
                break;

            // Terminate the string
            buf[iChars] = 0;

            // Add to the final output
            strOutput.append(buf);
        }

        SSL_shutdown(ssl);
        SSL_CTX_free(ctx);
        BIO_free_all(bio);
    }

private:
};

主程序调用类方法

  while(1)
    {
        for(int a = 0; a < m_vHostNames.size(); a++)
        {
            std::string strOutput = CConnector::GetData(m_vHostNames[a]);

            // Process the data
        }

        sleep(10000);
    }

调试器/分析器输出:

OpenSSL Mem Alloc

enter image description here 题: 我是否正确释放了OpenSSL?或者还有其他需要的东西?

感谢您对此的任何意见。

0 个答案:

没有答案