Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Memory Leak: Possible lost in sw::redis::Redis::get? #609

Open
Hoohaha opened this issue Nov 28, 2024 · 2 comments
Open

[BUG] Memory Leak: Possible lost in sw::redis::Redis::get? #609

Hoohaha opened this issue Nov 28, 2024 · 2 comments

Comments

@Hoohaha
Copy link

Hoohaha commented Nov 28, 2024

Describe the bug
Use Valgrind to analsis memory leak.
It report possible lost in location

==2611148== 2,028 bytes in 2 blocks are possibly lost in loss record 749 of 764
==2611148==    at 0x484DCD3: realloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==2611148==    by 0x4863B4F: hi_realloc (alloc.h:71)
==2611148==    by 0x4863B4F: sdsMakeRoomFor (sds.c:226)
==2611148==    by 0x486436B: sdscatlen (sds.c:384)
==2611148==    by 0x4869C1C: redisReaderFeed (read.c:724)
==2611148==    by 0x4862D21: redisBufferRead (hiredis.c:985)
==2611148==    by 0x48630AF: redisGetReply (hiredis.c:1081)
==2611148==    by 0x48B64C6: sw::redis::Connection::recv(bool) (in /usr/local/lib/libredis++.so.1.3.14)
==2611148==    by 0x48CBDBC: std::enable_if<!std::is_convertible<void (*)(sw::redis::Connection&, std::basic_string_view<char, std::char_traits<char> > const&), std::basic_string_view<char, std::char_traits<char> > >::value, std::unique_ptr<redisReply, sw::redis::ReplyDeleter> >::type sw::redis::Redis::command<void (*)(sw::redis::Connection&, std::basic_string_view<char, std::char_traits<char> > const&), std::basic_string_view<char, std::char_traits<char> > const&>(void (*)(sw::redis::Connection&, std::basic_string_view<char, std::char_traits<char> > const&), std::basic_string_view<char, std::char_traits<char> > const&) (in /usr/local/lib/libredis++.so.1.3.14)
==2611148==    by 0x48C245F: sw::redis::Redis::get[abi:cxx11](std::basic_string_view<char, std::char_traits<char> > const&) (in /usr/local/lib/libredis++.so.1.3.14)

To Reproduce
Minimal code to reproduce the bug.

    sw::redis::ConnectionPoolOptions poolOptions;
    poolOptions.size = 100;
    poolOptions.wait_timeout = std::chrono::milliseconds(100);
    poolOptions.connection_lifetime = std::chrono::minutes(10); // Max lifetime of a connection

    // Redis URI schema: tcp://[[username:]password@]host[:port][/db]
    auto uri = sw::redis::Uri("redis://default:redisadmin123@localhost:6379/1");
    sw::redis::Redis redis (uri.connection_options(), poolOptions);

// sample code
int CheckStatus<T>::match(shared_ptr<T>& item) {
        // check key's value in redis db
        string key = fmt::format(":1:{0}/mi", item->m_mac);
        auto val =  redis->get(key);

        if (val){
            item->m_isok= *val == "1" ? true: false;
        }
        else{
            item->m_isok= false;
        }

}
...


Expected behavior
A clear and concise description of what you expected to happen.

Environment:

  • OS: [e.g. ubuntu]
  • Compiler: 13.1.0
  • hiredis version:1.2.0
  • redis-plus-plus version: 1.3.14

Additional context

redis-plus-plus build with cmake .. -DREDIS_PLUS_PLUS_CXX_STANDARD=20

-- redis-plus-plus version: 1.3.14
-- The CXX compiler identification is GNU 13.1.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- redis-plus-plus build type: Release
-- redis-plus-plus build with CXX standard: c++20
-- redis-plus-plus TLS support: OFF
-- redis-plus-plus check hiredis features
-- Looking for redisEnableKeepAliveWithInterval
-- Looking for redisEnableKeepAliveWithInterval - found
-- redis-plus-plus build static library: ON
-- redis-plus-plus build static library with position independent code: ON
-- redis-plus-plus build shared library: ON
-- redis-plus-plus build test: ON
-- The C compiler identification is GNU 13.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
-- Debian package name: .deb
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xxxx/redis-plus-plus/build

Is it a memory leak?

@Hoohaha Hoohaha changed the title [BUG] Possible lost in sw::redis::Redis::get [BUG] Memory Leak: Possible lost in sw::redis::Redis::get? Nov 28, 2024
@sewenew
Copy link
Owner

sewenew commented Nov 29, 2024

Sorry, but I cannot reproduce your problem with the following code (since your code cannot compile, I modified it):

#include <iostream>
#include <string>
#include <sw/redis++/redis++.h>

using namespace std;

int main() {
        sw::redis::ConnectionPoolOptions poolOptions;
        poolOptions.size = 100;
        poolOptions.wait_timeout = std::chrono::milliseconds(100);
        poolOptions.connection_lifetime = std::chrono::minutes(10); // Max lifetime of a connection

        // Redis URI schema: tcp://[[username:]password@]host[:port][/db]
        auto uri = sw::redis::Uri("redis://localhost:6379/1");
        sw::redis::Redis redis (uri.connection_options(), poolOptions);
        string key = "test";
        auto val =  redis.get(key);
        if (val){
                cout << *val << endl;
        } else {
                cout << "not found" << endl;
        }
        return 0;
}

Can you try this code with your installation? Or give me code snippet that can be compiled without any third party dependency to reproduce the problem?

Regards

@sewenew
Copy link
Owner

sewenew commented Nov 29, 2024

By the way, redis.Uri is internal implementation, you'd better not call it in your application code. You can use the following code to achieve your goal:

sw::redis::Redis redis("redis://default:redisadmin123@localhost:6379/1?pool_size=100&pool_wait_timeout=100ms&pool_connection_lifetime=10m")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants