Skip to content
This repository has been archived by the owner on Jun 22, 2021. It is now read-only.

Memory leak #197

Open
zhaowq32 opened this issue Jun 7, 2021 · 0 comments
Open

Memory leak #197

zhaowq32 opened this issue Jun 7, 2021 · 0 comments

Comments

@zhaowq32
Copy link

zhaowq32 commented Jun 7, 2021

  • Version:1.41.0
  • Platform: Linux
  • Calling uv_queue_work will cause a memory leak, but it will not be executed in the main thread.

Code

#include <openssl/ssl.h>
#include <uv.h>

void WorkCallback(uv_work_t *req) {
    SSL_CTX * ctx = SSL_CTX_new(SSLv23_client_method());
    SSL_CTX_free(ctx);
    fprintf(stdout, "WorkCallback\n");
}

void AfterWorkCallback(uv_work_t *req, int status) {
    fprintf(stdout, "AfterWorkCallback\n");
}

int main() {
    uv_loop_t *loop = (uv_loop_t *) malloc(sizeof(uv_loop_t));
    uv_loop_init(loop);
    uv_work_t req1,req2;
    uv_queue_work(loop, &req1, WorkCallback, AfterWorkCallback);
    //uv_queue_work(loop, &req2, WorkCallback, AfterWorkCallback);
    uv_run(loop, UV_RUN_DEFAULT);
    uv_loop_close(loop);
    free(loop);
    /*
    SSL_CTX * ctx = SSL_CTX_new(SSLv23_client_method());
    SSL_CTX_free(ctx);
     */
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.19)
project(httplib_demo)

set(CMAKE_CXX_STANDARD 14)

if (NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Debug)
endif ()

SET(CMAKE_CXX_FLAGS_RELEASE "-O3")
SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -DDEBUG")

set(CMAKE_CXX_COMPILER /opt/rh/devtoolset-8/root/usr/bin/c++)
set(LIBUV_PATH /root/libs/libuv)
set(OPENSSL_PATH /root/libs/openssl)
link_directories(${LIBUV_PATH}/lib64)
include_directories(${LIBUV_PATH}/include)
include_directories(${OPENSSL_PATH}/include)
link_directories(${OPENSSL_PATH}/lib)
add_executable(httplib_demo main.cpp)
target_link_libraries(httplib_demo uv_a ssl crypto pthread dl)
target_compile_definitions(httplib_demo PUBLIC -DCPPHTTPLIB_OPENSSL_SUPPORT)

valgrind log

==155613== Memcheck, a memory error detector
==155613== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==155613== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==155613== Command: ./httplib_demo
==155613== 
WorkCallback
AfterWorkCallback
==155613== 
==155613== HEAP SUMMARY:
==155613==     in use at exit: 4,164 bytes in 20 blocks
==155613==   total heap usage: 4,626 allocs, 4,606 frees, 173,387 bytes allocated
==155613== 
==155613== 12 bytes in 1 blocks are definitely lost in loss record 1 of 20
==155613==    at 0x4C2B067: malloc (vg_replace_malloc.c:380)
==155613==    by 0x4EFDAF: CRYPTO_malloc (mem.c:222)
==155613==    by 0x4EFDE2: CRYPTO_zalloc (mem.c:230)
==155613==    by 0x4EC09F: ossl_init_get_thread_local (init.c:66)
==155613==    by 0x4EC4E7: ossl_init_thread_start (init.c:465)
==155613==    by 0x4CB96F: ERR_get_state (err.c:739)
==155613==    by 0x4CABA9: ERR_clear_error (err.c:445)
==155613==    by 0x606606: engine_load_rdrand_int (eng_rdrand.c:90)
==155613==    by 0x4EC3A4: ossl_init_engine_rdrand (init.c:353)
==155613==    by 0x4EC393: ossl_init_engine_rdrand_ossl_ (init.c:347)
==155613==    by 0x4E4120A: __pthread_once_slow (in /usr/lib64/libpthread-2.17.so)
==155613==    by 0x54B042: CRYPTO_THREAD_run_once (threads_pthread.c:118)
==155613== 
==155613== 584 bytes in 1 blocks are definitely lost in loss record 18 of 20
==155613==    at 0x4C2B067: malloc (vg_replace_malloc.c:380)
==155613==    by 0x4EFDAF: CRYPTO_malloc (mem.c:222)
==155613==    by 0x4EFDE2: CRYPTO_zalloc (mem.c:230)
==155613==    by 0x4CB942: ERR_get_state (err.c:734)
==155613==    by 0x4CABA9: ERR_clear_error (err.c:445)
==155613==    by 0x606606: engine_load_rdrand_int (eng_rdrand.c:90)
==155613==    by 0x4EC3A4: ossl_init_engine_rdrand (init.c:353)
==155613==    by 0x4EC393: ossl_init_engine_rdrand_ossl_ (init.c:347)
==155613==    by 0x4E4120A: __pthread_once_slow (in /usr/lib64/libpthread-2.17.so)
==155613==    by 0x54B042: CRYPTO_THREAD_run_once (threads_pthread.c:118)
==155613==    by 0x4ECA57: OPENSSL_init_crypto (init.c:723)
==155613==    by 0x603BBA: ENGINE_load_builtin_engines (eng_all.c:18)
==155613== 
==155613== 1,784 (376 direct, 1,408 indirect) bytes in 1 blocks are definitely lost in loss record 19 of 20
==155613==    at 0x4C2B067: malloc (vg_replace_malloc.c:380)
==155613==    by 0x4EFDAF: CRYPTO_malloc (mem.c:222)
==155613==    by 0x4EFDE2: CRYPTO_zalloc (mem.c:230)
==155613==    by 0x4F021B: CRYPTO_secure_zalloc (mem_sec.c:147)
==155613==    by 0x50DD4C: rand_drbg_new (drbg_lib.c:191)
==155613==    by 0x50DF73: RAND_DRBG_secure_new (drbg_lib.c:257)
==155613==    by 0x50F0FC: drbg_setup (drbg_lib.c:873)
==155613==    by 0x50F60D: RAND_DRBG_get0_public (drbg_lib.c:1119)
==155613==    by 0x50F2D7: drbg_bytes (drbg_lib.c:962)
==155613==    by 0x510BC5: RAND_bytes (rand_lib.c:941)
==155613==    by 0x43CAF1: SSL_CTX_new (ssl_lib.c:3106)
==155613==    by 0x40615A: WorkCallback(uv_work_s*) (main.cpp:5)
==155613== 
==155613== 1,784 (376 direct, 1,408 indirect) bytes in 1 blocks are definitely lost in loss record 20 of 20
==155613==    at 0x4C2B067: malloc (vg_replace_malloc.c:380)
==155613==    by 0x4EFDAF: CRYPTO_malloc (mem.c:222)
==155613==    by 0x4EFDE2: CRYPTO_zalloc (mem.c:230)
==155613==    by 0x4F021B: CRYPTO_secure_zalloc (mem_sec.c:147)
==155613==    by 0x50DD4C: rand_drbg_new (drbg_lib.c:191)
==155613==    by 0x50DF73: RAND_DRBG_secure_new (drbg_lib.c:257)
==155613==    by 0x50F0FC: drbg_setup (drbg_lib.c:873)
==155613==    by 0x50F695: RAND_DRBG_get0_private (drbg_lib.c:1140)
==155613==    by 0x510B56: RAND_priv_bytes (rand_lib.c:929)
==155613==    by 0x43CB0D: SSL_CTX_new (ssl_lib.c:3108)
==155613==    by 0x40615A: WorkCallback(uv_work_s*) (main.cpp:5)
==155613==    by 0x406304: worker (in /root/httplib_demo/build/httplib_demo)
==155613== 
==155613== LEAK SUMMARY:
==155613==    definitely lost: 1,348 bytes in 4 blocks
==155613==    indirectly lost: 2,816 bytes in 16 blocks
==155613==      possibly lost: 0 bytes in 0 blocks
==155613==    still reachable: 0 bytes in 0 blocks
==155613==         suppressed: 0 bytes in 0 blocks
==155613== 
==155613== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)
@bnoordhuis bnoordhuis transferred this issue from libuv/libuv Jun 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant