简体   繁体   English

使用Valgrind的hiredis内存泄漏

[英]memory leak in hiredis using valgrind

I ran valgrind on my code which uses hiredis, it points out the following individual lines in my code : 我在使用hiredis的代码上运行valgrind,它指出了我代码中的以下几行:

  • redisAsyncConnect() redisAsyncConnect()
  • redisAsyncConnectUnix() redisAsyncConnectUnix()
  • redisLibuvAttach() redisLibuvAttach()
  • uv_loop_new() 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. 我已经使用'redisAsyncDisconnect'来释放前两种情况的内存,找不到用于第三种情况的正确方法。 For the fourth one i used uv_stop(). 对于第四个,我使用了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 ? 但是valgrind仍然说这四个内存中肯定都有内存丢失,释放内存的正确方法是什么?

Just doing a simple google search shows the method redisLibuvAttach() just does a simple malloc 只是做一个简单的谷歌搜索显示方法redisLibuvAttach() 只是做一个简单的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) : 该文件中的on_close方法显示您可以简单地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. 或者只是确保该方法被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM