Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

内存泄露 #256

Open
NongGuangXin opened this issue May 6, 2024 · 2 comments
Open

内存泄露 #256

NongGuangXin opened this issue May 6, 2024 · 2 comments

Comments

@NongGuangXin
Copy link

修改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
Copy link

JavenLaw commented Jun 1, 2024

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

@zhouyunbin
Copy link

zhouyunbin commented Jun 1, 2024 via email

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants