redis freeReplyObject 不释放内存

redis freeReplyObject not freeing memory

代码如下:

redisReply * reply;
char * stats = get_key(key, reply);
freeReplyObject( reply );

get_key是单独头文件中的函数:

char * get_key(const char* key, redisReply * reply)
{
    reply = redisCommand(rc, "GET %s", key);
    if (reply->type != REDIS_REPLY_ERROR) {
        return reply->str;
    } else {
        return "ERROR";
    }
}

Valgrind 是这样说的:

==24846== 2,592 bytes in 54 blocks are definitely lost in loss record 63 of 85
==24846==    at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==24846==    by 0x4E3E342: createReplyObject (hiredis.c:64)
==24846==    by 0x4E3E342: createNilObject (hiredis.c:179)
==24846==    by 0x4E469FE: processBulkItem (read.c:267)
==24846==    by 0x4E469FE: processItem (read.c:407)
==24846==    by 0x4E469FE: redisReaderGetReply (read.c:503)
==24846==    by 0x4E406E3: redisGetReplyFromReader (hiredis.c:863)
==24846==    by 0x4E407CA: redisGetReply (hiredis.c:890)
==24846==    by 0x409DEE: RedisCluster::HiredisCommand<RedisCluster::Cluster<redisContext, RedisCluster::DefaultContainer<redisContext> > >::processHiredisCommand(redisContext*) (in /bucket)
==24846==    by 0x408E3A: RedisCluster::HiredisCommand<RedisCluster::Cluster<redisContext, RedisCluster::DefaultContainer<redisContext> > >::process() (in /bucket)
==24846==    by 0x408818: RedisCluster::HiredisCommand<RedisCluster::Cluster<redisContext, RedisCluster::DefaultContainer<redisContext> > >::Command(RedisCluster::Cluster<redisContext, RedisCluster::DefaultContainer<redisContext> >*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char const*, ...) (in /bucket)
==24846==    by 0x405104: get_key(char const*, redisReply*) (in /bucket)
==24846==    by 0x406013: main (in /bucket)

我猜这与我传递回复指针的方式有关,但我无法真正说出我做错了什么。

您没有为 redisReply *reply 分配内存,在调用 get_key 函数之前您必须分配内存。

正如第一条评论所说,你有一个间接的问题。

如果我重写你的代码但内联 get_key 并将 get_key 的形式参数从 reply 重命名为 replyLocal 并且只专注于 reply我们得到的变量

redisReply * reply;
//char * stats = get_key(key, reply);
{
// this is what happens in the function call, reply is copied in to replyLocal
char* replyLocal = reply;
// and this is in the body of the function
replyLocal = redisCommand(rc, "GET %s", key);
// if and return ignored
}

我希望清楚 replyLocal 的赋值不会修改 reply。这意味着当您尝试释放内存时,它并没有像预期的那样工作。

如果你把get_key的签名改成

char * get_key(const char* key, redisReply ** reply)

并使正文适应额外的间接级别,从

开始
*reply = redisCommand(rc, "GET %s", key);

然后调用变为

char * stats = get_key(key, &reply);

应用与上面相同的内联

redisReply * reply;
//char * stats = get_key(key, &reply);
{
// this is what happens in the function call,
// ** the address of ** reply is copied in to replyLocal
char** replyLocal = &reply;
// and this is in the body of the function
*replyLocal = redisCommand(rc, "GET %s", key);
// if and return ignored
}

这现在修改了 replyLocal 指向的内容,因此它也修改了 reply