sewenew / redis-plus-plus

Redis client written in C++
Apache License 2.0
1.64k stars 351 forks source link

[BUG] It seems that AsyncRedisCluster has a memory leak #471

Closed nqf closed 1 year ago

nqf commented 1 year ago
valgrind --log-file=a.txt --tool=memcheck --leak-check=full -s  --track-origins=yes ./test

==40353== Memcheck, a memory error detector
==40353== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==40353== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==40353== Command: ./test
==40353== Parent PID: 43898
==40353== 
==40353== 
==40353== HEAP SUMMARY:
==40353==     in use at exit: 2,022 bytes in 25 blocks
==40353==   total heap usage: 446 allocs, 421 frees, 39,614 bytes allocated
==40353== 
==40353== 2,022 (240 direct, 1,782 indirect) bytes in 1 blocks are definitely lost in loss record 17 of 17
==40353==    at 0x4C2B788: operator new(unsigned long) (vg_replace_malloc.c:417)
==40353==    by 0x661250: sw::redis::AsyncConnectionPool::_create() (in /home/fantasy-peak/test/build/test)
==40353==    by 0x662ED0: sw::redis::AsyncConnectionPool::fetch() (in /home/fantasy-peak/test/build/test)
==40353==    by 0x65DE04: sw::redis::GuardedAsyncConnection::GuardedAsyncConnection(std::shared_ptr<sw::redis::AsyncConnectionPool> const&) (in /home/fantasy-peak/test/build/test)
==40353==    by 0x42FE1B: _callback_command_with_parser<std::optional<std::basic_string<char> >, sw::redis::DefaultResultParser<std::optional<std::basic_string<char> > >, main(int, char**)::<lambda(std::future<std::optional<std::basic_string<char> > >&&)>, sw::redis::FormattedCommand (*)(const std::basic_string_view<char>&), const std::basic_string_view<char, std::char_traits<char> >&> (async_redis_cluster.h:1148)
==40353==    by 0x42FE1B: _callback_generic_command<std::optional<std::basic_string<char> >, main(int, char**)::<lambda(std::future<std::optional<std::basic_string<char> > >&&)>, sw::redis::FormattedCommand (*)(const std::basic_string_view<char>&), const std::basic_string_view<char, std::char_traits<char> >&> (async_redis_cluster.h:1133)
==40353==    by 0x42FE1B: _callback_generic_command<std::optional<std::basic_string<char> >, main(int, char**)::<lambda(std::future<std::optional<std::basic_string<char> > >&&)>, sw::redis::FormattedCommand (*)(const std::basic_string_view<char>&)> (async_redis_cluster.h:1118)
==40353==    by 0x42FE1B: _callback_fmt_command<std::optional<std::basic_string<char> >, main(int, char**)::<lambda(std::future<std::optional<std::basic_string<char> > >&&)>, sw::redis::FormattedCommand (*)(const std::basic_string_view<char>&), const std::basic_string_view<char>&> (async_redis_cluster.h:1108)
==40353==    by 0x42FE1B: get<main(int, char**)::<lambda(std::future<std::optional<std::basic_string<char> > >&&)> > (async_redis_cluster.h:241)
==40353==    by 0x42FE1B: main (main.cpp:17)
==40353== 
==40353== LEAK SUMMARY:
==40353==    definitely lost: 240 bytes in 1 blocks
==40353==    indirectly lost: 1,782 bytes in 24 blocks
==40353==      possibly lost: 0 bytes in 0 blocks
==40353==    still reachable: 0 bytes in 0 blocks
==40353==         suppressed: 0 bytes in 0 blocks
==40353== 
==40353== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

--------------------------------------------------------------------------------------------------

#include <iostream>

#include <sw/redis++/async_redis_cluster.h>

int main(int argc, char** argv) {
    (void)argc;
    (void)argv;
    sw::redis::ConnectionOptions connection_opts;
    connection_opts.connect_timeout = std::chrono::milliseconds(100);
    connection_opts.socket_timeout = std::chrono::milliseconds(100);
    connection_opts.host = "127.0.0.1";
    connection_opts.port = 7000;
    sw::redis::ConnectionPoolOptions pool_opts;
    pool_opts.size = 3; // Optional. The default size is 1.
    auto async_cluster = sw::redis::AsyncRedisCluster(connection_opts, pool_opts);
    async_cluster.get("hello", [](std::future<std::optional<std::string>>&& fut) {
        try {
            auto os = fut.get();
            std::cout << os.value() << std::endl;
        } catch (const sw::redis::Error& e) {
            std::cout << "lll:" << e.what() << std::endl;
        }
    });
    sleep(10);
}
sewenew commented 1 year ago

Might be duplicated with #442 . Could you please test with the latest code on master branch? This problem should have been fixed.

Regards

nqf commented 1 year ago

Might be duplicated with #442 . Could you please test with the latest code on master branch? This problem should have been fixed.

Regards

thanks