hiredis memory leak test by valgrind

Created on 19 Sep 2017  Â·  6Comments  Â·  Source: redis/hiredis

hello, I am using hiredis library in my C++ service , but have occur memory leak. the valgring output like this:
==11465== 867 (816 direct, 51 indirect) bytes in 17 blocks are definitely lost in loss record 127 of 158
==11465== at 0x4C27F6F: calloc (vg_replace_malloc.c:623)
==11465== by 0x5C8DF67: createReplyObject (hiredis.c:64)
==11465== by 0x5C8DF67: createStringObject (hiredis.c:106)
==11465== by 0x5C9550C: processLineItem (read.c:230)
==11465== by 0x5C9550C: processItem (read.c:405)
==11465== by 0x5C9550C: redisReaderGetReply (read.c:503)
==11465== by 0x5C8E173: redisGetReplyFromReader (hiredis.c:863)
==11465== by 0x5C8ECEA: redisGetReply (hiredis.c:890)
==11465== by 0x5C8FEA4: __redisBlockForReply (hiredis.c:995)
==11465== by 0x5C8FEA4: redisvCommand (hiredis.c:1005)
==11465== by 0x5C8FF37: redisCommand (hiredis.c:1011)

Most helpful comment

In this code :
void Redis::selectdb(int db)
{
redisCommand(_connect, "SELECT %d", db);
}

the redisCommand return value not free;

Right approach:

redisReply *reply;
reply = redisCommand(_connect, "SELECT %d", db);
if (reply)
{
freeReplyObject(reply);
}

2017-09-19 10:58 GMT+08:00 gameyin notifications@github.com:

hello, I am using hiredis library in my C++ service , but have occur
memory leak. the valgring output like this:
==11465== 867 (816 direct, 51 indirect) bytes in 17 blocks are definitely
lost in loss record 127 of 158
==11465== at 0x4C27F6F: calloc (vg_replace_malloc.c:623)
==11465== by 0x5C8DF67: createReplyObject (hiredis.c:64)
==11465== by 0x5C8DF67: createStringObject (hiredis.c:106)
==11465== by 0x5C9550C: processLineItem (read.c:230)
==11465== by 0x5C9550C: processItem (read.c:405)
==11465== by 0x5C9550C: redisReaderGetReply (read.c:503)
==11465== by 0x5C8E173: redisGetReplyFromReader (hiredis.c:863)
==11465== by 0x5C8ECEA: redisGetReply (hiredis.c:890)
==11465== by 0x5C8FEA4: __redisBlockForReply (hiredis.c:995)
==11465== by 0x5C8FEA4: redisvCommand (hiredis.c:1005)
==11465== by 0x5C8FF37: redisCommand (hiredis.c:1011)

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/redis/hiredis/issues/546, or mute the thread
https://github.com/notifications/unsubscribe-auth/AFz9paGGNFCWQQYtW7bRlQlL-EHINXh6ks5sjy3ggaJpZM4PbyeT
.

All 6 comments

Can you provide a small script that causes the problem? There is no way to know if that call stack shows a leak in hiredis or in how you're calling it.

Are you sure you're calling freeReplyObject each time you retrieve a reply?

like this:
==11465== 1,020 (960 direct, 60 indirect) bytes in 20 blocks are definitely lost in loss record 128 of 158
==11465== at 0x4C27F6F: calloc (vg_replace_malloc.c:623)
==11465== by 0x5C8DF67: createReplyObject (hiredis.c:64)
==11465== by 0x5C8DF67: createStringObject (hiredis.c:106)
==11465== by 0x5C9550C: processLineItem (read.c:230)
==11465== by 0x5C9550C: processItem (read.c:405)
==11465== by 0x5C9550C: redisReaderGetReply (read.c:503)
==11465== by 0x5C8E173: redisGetReplyFromReader (hiredis.c:863)
==11465== by 0x5C8ECEA: redisGetReply (hiredis.c:890)
==11465== by 0x5C8FEA4: __redisBlockForReply (hiredis.c:995)
==11465== by 0x5C8FEA4: redisvCommand (hiredis.c:1005)
==11465== by 0x5C8FF37: redisCommand (hiredis.c:1011)
==11465== by 0x477A64: Redis::selectdb(int) (redis.cpp:100)
==11465== by 0x4781C3: Redis::hset_get(int, std::string const&, std::string const&) (redis.cpp:224)

and in my redis.cpp:

void Redis::selectdb(int db)
{
redisCommand(_connect, "SELECT %d", db);
}

string Redis::hset_get(int db, const string& key, const string& field)
{
std::string str="";
selectdb(db);
_reply = (redisReply*)redisCommand(_connect, "HGET %s %s", key.c_str(), field.c_str());
if (_reply->len > 0)
{
str = _reply->str;
}
freeReplyObject(_reply);
return str;
}

does select db also need to freeReplyObject ?

I get it, thanks

In this code :
void Redis::selectdb(int db)
{
redisCommand(_connect, "SELECT %d", db);
}

the redisCommand return value not free;

Right approach:

redisReply *reply;
reply = redisCommand(_connect, "SELECT %d", db);
if (reply)
{
freeReplyObject(reply);
}

2017-09-19 10:58 GMT+08:00 gameyin notifications@github.com:

hello, I am using hiredis library in my C++ service , but have occur
memory leak. the valgring output like this:
==11465== 867 (816 direct, 51 indirect) bytes in 17 blocks are definitely
lost in loss record 127 of 158
==11465== at 0x4C27F6F: calloc (vg_replace_malloc.c:623)
==11465== by 0x5C8DF67: createReplyObject (hiredis.c:64)
==11465== by 0x5C8DF67: createStringObject (hiredis.c:106)
==11465== by 0x5C9550C: processLineItem (read.c:230)
==11465== by 0x5C9550C: processItem (read.c:405)
==11465== by 0x5C9550C: redisReaderGetReply (read.c:503)
==11465== by 0x5C8E173: redisGetReplyFromReader (hiredis.c:863)
==11465== by 0x5C8ECEA: redisGetReply (hiredis.c:890)
==11465== by 0x5C8FEA4: __redisBlockForReply (hiredis.c:995)
==11465== by 0x5C8FEA4: redisvCommand (hiredis.c:1005)
==11465== by 0x5C8FF37: redisCommand (hiredis.c:1011)

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/redis/hiredis/issues/546, or mute the thread
https://github.com/notifications/unsubscribe-auth/AFz9paGGNFCWQQYtW7bRlQlL-EHINXh6ks5sjy3ggaJpZM4PbyeT
.

good

Was this page helpful?
0 / 5 - 0 ratings