redis freeReplyObject没有释放内存

时间:2017-09-21 06:09:49

标签: c valgrind hiredis

以下是代码:

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)

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

2 个答案:

答案 0 :(得分:1)

如第一条评论所述,你有间接问题。

如果我重写您的代码但内联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 的分配不会修改回复。这意味着当您尝试释放内存时,它无法正常工作。

如果您将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指向的内容,因此它也修改了回复

答案 1 :(得分:0)

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