2 dulin19870504 dulin19870504 于 2017.09.11 15:36 提问

Centos6.5 使用curl时 用valgrind 检查内存泄露

测试源码如下

#include "stdio.h"
#include <iostream>

#include <curl/curl.h>

#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/conf.h>
#include <openssl/ssl.h>
#include <openssl/rand.h>


#define LOG_STD(p) std::cout<<__FUNCTION__<<" "<<p<<"["<<__FILE__<<":"<<__LINE__<<"]"<<std::endl

int main(int argc, char* argv[])
{
    LOG_STD("test--begin");

    CURLcode code = curl_global_init(CURL_GLOBAL_SSL);
    if (code != CURLE_OK)
    {
        LOG_STD("curl_global_init fail");
    }
    LOG_STD("test--version:"<<curl_version());
    sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
    CRYPTO_cleanup_all_ex_data();
    CONF_modules_free();
    ERR_remove_state(0);
    CONF_modules_unload(1); 
    ERR_free_strings();
    ERR_remove_thread_state(NULL);
    EVP_cleanup();
    curl_global_cleanup();

    LOG_STD("test--begin");
}

直接测试结果如下

 [root@localhost curl_test]# valgrind --leak-check=full ./a.out 
==4897== Memcheck, a memory error detector
==4897== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4897== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==4897== Command: ./a.out
==4897== 
main test--begin[Test.cpp:18]
main test--version:libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2[Test.cpp:25]
main test--begin[Test.cpp:36]
==4897== 
==4897== HEAP SUMMARY:
==4897==     in use at exit: 7,776 bytes in 78 blocks
==4897==   total heap usage: 88 allocs, 10 frees, 8,891 bytes allocated
==4897== 
==4897== LEAK SUMMARY:
==4897==    definitely lost: 0 bytes in 0 blocks
==4897==    indirectly lost: 0 bytes in 0 blocks
==4897==      possibly lost: 0 bytes in 0 blocks
==4897==    still reachable: 7,776 bytes in 78 blocks
==4897==         suppressed: 0 bytes in 0 blocks
==4897== Reachable blocks (those to which a pointer was found) are not shown.
==4897== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==4897== 
==4897== For counts of detected and suppressed errors, rerun with: -v
==4897== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)

更新curl版本后测试如下

 [root@localhost curl_test]# valgrind --leak-check=full --show-leak-kinds=all ./a.out 
==4979== Memcheck, a memory error detector
==4979== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4979== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==4979== Command: ./a.out
==4979== 
main test--begin[Test.cpp:18]
main test--version:libcurl/7.55.1 OpenSSL/1.0.1e zlib/1.2.3 c-ares/1.13.0 libssh2/1.8.0 nghttp2/1.6.0[Test.cpp:25]
main test--begin[Test.cpp:36]
==4979== 
==4979== HEAP SUMMARY:
==4979==     in use at exit: 416 bytes in 6 blocks
==4979==   total heap usage: 4,206 allocs, 4,200 frees, 142,966 bytes allocated
==4979== 
==4979== 24 bytes in 1 blocks are still reachable in loss record 1 of 6
==4979==    at 0x4A0728A: malloc (vg_replace_malloc.c:299)
==4979==    by 0x301566AC2D: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x301566C5E5: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x301566C6A8: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D650F: engine_free_util (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D6B8F: ENGINE_remove (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D6C74: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D63B5: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156E5A8F: sk_pop_free (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D638B: ENGINE_cleanup (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x4C82CDD: ??? (in /usr/lib64/libcurl.so.4.4.0)
==4979==    by 0x4C85D31: ??? (in /usr/lib64/libcurl.so.4.4.0)
==4979== 
==4979== 24 bytes in 1 blocks are still reachable in loss record 2 of 6
==4979==    at 0x4A0728A: malloc (vg_replace_malloc.c:299)
==4979==    by 0x301566AC2D: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156E6302: lh_insert (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x301566C617: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x301566C6A8: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D650F: engine_free_util (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D6B8F: ENGINE_remove (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D6C74: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D63B5: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156E5A8F: sk_pop_free (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D638B: ENGINE_cleanup (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x4C82CDD: ??? (in /usr/lib64/libcurl.so.4.4.0)
==4979== 
==4979== 32 bytes in 1 blocks are still reachable in loss record 3 of 6
==4979==    at 0x4A0728A: malloc (vg_replace_malloc.c:299)
==4979==    by 0x301566AC2D: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156E5C9E: sk_new (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x301566C5FB: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x301566C6A8: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D650F: engine_free_util (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D6B8F: ENGINE_remove (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D6C74: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D63B5: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156E5A8F: sk_pop_free (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D638B: ENGINE_cleanup (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x4C82CDD: ??? (in /usr/lib64/libcurl.so.4.4.0)
==4979== 
==4979== 32 bytes in 1 blocks are still reachable in loss record 4 of 6
==4979==    at 0x4A0728A: malloc (vg_replace_malloc.c:299)
==4979==    by 0x301566AC2D: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156E5CBC: sk_new (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x301566C5FB: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x301566C6A8: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D650F: engine_free_util (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D6B8F: ENGINE_remove (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D6C74: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D63B5: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156E5A8F: sk_pop_free (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D638B: ENGINE_cleanup (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x4C82CDD: ??? (in /usr/lib64/libcurl.so.4.4.0)
==4979== 
==4979== 128 bytes in 1 blocks are still reachable in loss record 5 of 6
==4979==    at 0x4A0728A: malloc (vg_replace_malloc.c:299)
==4979==    by 0x301566AC2D: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156E6391: lh_new (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x301566C524: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x301566C674: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x301566C6A8: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D650F: engine_free_util (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D6B8F: ENGINE_remove (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D6C74: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D63B5: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156E5A8F: sk_pop_free (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D638B: ENGINE_cleanup (in /usr/lib64/libcrypto.so.1.0.1e)
==4979== 
==4979== 176 bytes in 1 blocks are still reachable in loss record 6 of 6
==4979==    at 0x4A0728A: malloc (vg_replace_malloc.c:299)
==4979==    by 0x301566AC2D: CRYPTO_malloc (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156E636F: lh_new (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x301566C524: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x301566C674: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x301566C6A8: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D650F: engine_free_util (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D6B8F: ENGINE_remove (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D6C74: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D63B5: ??? (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156E5A8F: sk_pop_free (in /usr/lib64/libcrypto.so.1.0.1e)
==4979==    by 0x30156D638B: ENGINE_cleanup (in /usr/lib64/libcrypto.so.1.0.1e)
==4979== 
==4979== LEAK SUMMARY:
==4979==    definitely lost: 0 bytes in 0 blocks
==4979==    indirectly lost: 0 bytes in 0 blocks
==4979==      possibly lost: 0 bytes in 0 blocks
==4979==    still reachable: 416 bytes in 6 blocks
==4979==         suppressed: 0 bytes in 0 blocks
==4979== 
==4979== For counts of detected and suppressed errors, rerun with: -v
==4979== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
Csdn user default icon
上传中...
上传图片
插入图片