1

I ran valgrind on my code which uses hiredis, it points out the following individual lines in my code :

  • redisAsyncConnect()
  • redisAsyncConnectUnix()
  • redisLibuvAttach()
  • uv_loop_new()

I have used 'redisAsyncDisconnect' to free up the memory for the first two cases, couldn't find the right method for third one. For the fourth one i used uv_stop(). But still valgrind says there is definitely a loss in memory in all the four, what is the right way to release the memory ?

melwin_jose
  • 315
  • 3
  • 20

1 Answers1

1

Just doing a simple google search shows the method redisLibuvAttach() just does a simple malloc

static int redisLibuvAttach(redisAsyncContext* ac, uv_loop_t* loop) {
  redisContext *c = &(ac->c);

  if (ac->ev.data != NULL) {
    return REDIS_ERR;
  }

  ac->ev.addRead  = redisLibuvAddRead;
  ac->ev.delRead  = redisLibuvDelRead;
  ac->ev.addWrite = redisLibuvAddWrite;
  ac->ev.delWrite = redisLibuvDelWrite;
  ac->ev.cleanup  = redisLibuvCleanup;

  redisLibuvEvents* p = (redisLibuvEvents*)malloc(sizeof(*p));

  if (!p) {
    return REDIS_ERR;
  }

  m emset(p, 0, sizeof(*p));

  if (uv_poll_init(loop, &p->handle, c->fd) != 0) {
    return REDIS_ERR;
  }

  ac->ev.data    = p;
  p->handle.data = p;
  p->context     = ac;

  return REDIS_OK;
}

The method on_close in that file shows you can simply free(handle->data) :

static void on_close(uv_handle_t* handle) {
  redisLibuvEvents* p = (redisLibuvEvents*)handle->data;

  free(p);
}

Or just make sure that method is called.

Eregrith
  • 4,263
  • 18
  • 39
  • all that i have is a pointer to redisAsyncContext, using redisAsyncDisconnect() gives a segmentation fault and redisFree() works but shows up as memory lost in valgrind. Is there any hiredis function to release to properly ? – melwin_jose May 06 '15 at 04:20