HardySimpson / zlog

A reliable, high-performance, thread safe, flexsible, clear-model, pure C logging library.
Apache License 2.0
2.25k stars 721 forks source link

内存泄露 #256

Open NongGuangXin opened 6 months ago

NongGuangXin commented 6 months ago

修改test/test_hello.c内容为如下:

#include <stdio.h>
#include "zlog.h"

const char* conf = "[global]\n"
                   "default format = \"[\%d.\%ms \%m\%n\"\n"
                   "[formats]\n"
                   "dbgfmt = \"[\%d.\%ms \%-6V] \%m\%n\"\n"
                   "[rules]\n"
                   "ngx.*     \"/tmp/debug.log\", 10M * 10\n"
                   "ngx.INFO  >stdout";

int main(int argc, char** argv)
{
    int rc;
    zlog_category_t *zc;

    rc = zlog_init_from_string(conf);
    if (rc) {
        printf("init failed\n");
        return -1;
    }

    zc = zlog_get_category("ngx");
    if (!zc) {
        printf("get cat fail\n");
        zlog_fini();
        return -2;
    }

    zlog_info(zc, "hello, zlog");

    zlog_fini();

    return 0;
}

编译运行,显示内存泄露:

$ valgrind --tool=memcheck --leak-check=full ./test_hello 
==5506== Memcheck, a memory error detector
==5506== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5506== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==5506== Command: ./test_hello
==5506== 
[2024-05-06 09:31:06.768 hello, zlog
==5506== 
==5506== HEAP SUMMARY:
==5506==     in use at exit: 121,184 bytes in 16 blocks
==5506==   total heap usage: 91 allocs, 75 frees, 896,548 bytes allocated
==5506== 
==5506== 1,144 bytes in 1 blocks are definitely lost in loss record 3 of 5
==5506==    at 0x4837B65: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==5506==    by 0x484E01C: zlog_rotater_new (rotater.c:99)
==5506==    by 0x484C2C9: zlog_conf_new_from_string (conf.c:267)
==5506==    by 0x485445F: zlog_init_inner_from_string (zlog.c:98)
==5506==    by 0x485445F: zlog_init_from_string (zlog.c:231)
==5506==    by 0x10909C: main (test_hello.c:32)
==5506== 
==5506== 120,040 (8,208 direct, 111,832 indirect) bytes in 1 blocks are definitely lost in loss record 5 of 5
==5506==    at 0x4837B65: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==5506==    by 0x484C951: zlog_format_new (format.c:74)
==5506==    by 0x484C2B1: zlog_conf_new_from_string (conf.c:260)
==5506==    by 0x485445F: zlog_init_inner_from_string (zlog.c:98)
==5506==    by 0x485445F: zlog_init_from_string (zlog.c:231)
==5506==    by 0x10909C: main (test_hello.c:32)
==5506== 
==5506== LEAK SUMMARY:
==5506==    definitely lost: 9,352 bytes in 2 blocks
==5506==    indirectly lost: 111,832 bytes in 14 blocks
==5506==      possibly lost: 0 bytes in 0 blocks
==5506==    still reachable: 0 bytes in 0 blocks
==5506==         suppressed: 0 bytes in 0 blocks
==5506== 
==5506== For counts of detected and suppressed errors, rerun with: -v
==5506== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

测试环境:

JavenLaw commented 5 months ago

长时间运行的话,内存泄露会一直增长吗

zhouyunbin commented 5 months ago

您好,已收到您的来信

LuckyBellaCat commented 1 month ago

修改内容为如下:test/test_hello.c

#include <stdio.h>
#include "zlog.h"

const char* conf = "[global]\n"
                   "default format = \"[\%d.\%ms \%m\%n\"\n"
                   "[formats]\n"
                   "dbgfmt = \"[\%d.\%ms \%-6V] \%m\%n\"\n"
                   "[rules]\n"
                   "ngx.*     \"/tmp/debug.log\", 10M * 10\n"
                   "ngx.INFO  >stdout";

int main(int argc, char** argv)
{
  int rc;
  zlog_category_t *zc;

  rc = zlog_init_from_string(conf);
  if (rc) {
      printf("init failed\n");
      return -1;
  }

  zc = zlog_get_category("ngx");
  if (!zc) {
      printf("get cat fail\n");
      zlog_fini();
      return -2;
  }

  zlog_info(zc, "hello, zlog");

  zlog_fini();

  return 0;
}

编译运行,显示内存泄露:

$ valgrind --tool=memcheck --leak-check=full ./test_hello 
==5506== Memcheck, a memory error detector
==5506== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5506== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==5506== Command: ./test_hello
==5506== 
[2024-05-06 09:31:06.768 hello, zlog
==5506== 
==5506== HEAP SUMMARY:
==5506==     in use at exit: 121,184 bytes in 16 blocks
==5506==   total heap usage: 91 allocs, 75 frees, 896,548 bytes allocated
==5506== 
==5506== 1,144 bytes in 1 blocks are definitely lost in loss record 3 of 5
==5506==    at 0x4837B65: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==5506==    by 0x484E01C: zlog_rotater_new (rotater.c:99)
==5506==    by 0x484C2C9: zlog_conf_new_from_string (conf.c:267)
==5506==    by 0x485445F: zlog_init_inner_from_string (zlog.c:98)
==5506==    by 0x485445F: zlog_init_from_string (zlog.c:231)
==5506==    by 0x10909C: main (test_hello.c:32)
==5506== 
==5506== 120,040 (8,208 direct, 111,832 indirect) bytes in 1 blocks are definitely lost in loss record 5 of 5
==5506==    at 0x4837B65: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==5506==    by 0x484C951: zlog_format_new (format.c:74)
==5506==    by 0x484C2B1: zlog_conf_new_from_string (conf.c:260)
==5506==    by 0x485445F: zlog_init_inner_from_string (zlog.c:98)
==5506==    by 0x485445F: zlog_init_from_string (zlog.c:231)
==5506==    by 0x10909C: main (test_hello.c:32)
==5506== 
==5506== LEAK SUMMARY:
==5506==    definitely lost: 9,352 bytes in 2 blocks
==5506==    indirectly lost: 111,832 bytes in 14 blocks
==5506==      possibly lost: 0 bytes in 0 blocks
==5506==    still reachable: 0 bytes in 0 blocks
==5506==         suppressed: 0 bytes in 0 blocks
==5506== 
==5506== For counts of detected and suppressed errors, rerun with: -v
==5506== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

测试环境:

您好~我遇到了和您一样的问题,麻烦问下您是如何解决这个问题的?

zhouyunbin commented 1 month ago

您好,已收到您的来信