Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

程序析构时出现段错的问题 #36

Closed
hallelujah-shih opened this issue Jul 26, 2013 · 3 comments
Closed

程序析构时出现段错的问题 #36

hallelujah-shih opened this issue Jul 26, 2013 · 3 comments

Comments

@hallelujah-shih
Copy link

我将程序中的zlog更换为其他日志系统时,没问题,或是程序中不使用log时也没问题,使用zlog的时候每次主程序退出时将段错。我将zlog单独写个简单程序也没问题,我的链接库有pthread gtest zlog rt pcap,执行代码如下:
29 zlog_category_t *my_log;
30
31 int main(int argc, char *argv[])
32 {
33 if (zlog_init("analysistcp_log.conf"))
34 {
35 if (zlog_init(NULL))
36 return -1;
37 }
38
39 if ((my_log = zlog_get_category("analysistcp_main")) == NULL)
40 {
41 zlog_fini();
42 zlog_init(NULL);
43 my_log = zlog_get_category("analysistcp_main");
44 }
45
46 if (argc != 2)
47 {
48 zlog_error(my_log, "Usage: %s \n", argv[0]);
49 zlog_fini();
50 return 0;
51 }

详细的栈信息如下:
Program terminated with signal 11, Segmentation fault.
#0 __libc_free (mem=0x20) at malloc.c:3709

3709 if (chunk_is_mmapped(p)) /* release mmapped memory. */
(gdb) bt full
#0 __libc_free (mem=0x20) at malloc.c:3709

    ar_ptr = <value optimized out>
    p = <value optimized out>
    hook = 0

#1 0x00007f12fb181d0e in zc_hashtable_del (a_table=0x129d3c0) at zc_hashtable.c:80

    i = <value optimized out>
    p = <value optimized out>
    q = <value optimized out>

#2 0x00007f12fb17c1c6 in zlog_mdc_del (a_mdc=0x12c1000) at mdc.c:36

No locals.
#3 0x00007f12fb1812fb in zlog_thread_del (a_thread=0x129d470) at thread.c:45

No locals.
#4 0x00000036584369f1 in __run_exit_handlers (status=0) at exit.c:78

    atfct = <value optimized out>
    onfct = <value optimized out>
    cxafct = <value optimized out>

#5 exit (status=0) at exit.c:100

No locals.
#6 0x000000365841ee64 in __libc_start_main (main=0x41e8c0 <main(int, char**)>, argc=1, ubp_av=0x7fff4b937d38, init=,

fini=<value optimized out>, rtld_fini=<value optimized out>, stack_end=0x7fff4b937d28) at libc-start.c:258
    result = <value optimized out>
    unwind_buf = {cancel_jmp_buf = {{jmp_buf = {0, 401847908166741622, 4219392, 140734461345072, 0, 0, -401610168829006218, 
            432088316090986102}, mask_was_saved = 0}}, priv = {pad = {0x0, 0x0, 0x451e80, 0x7fff4b937d38}, data = {prev = 0x0, 
          cleanup = 0x0, canceltype = 4529792}}}
    not_first_call = <value optimized out>

#7 0x0000000000406229 in _start ()

No symbol table info available.

@HardySimpson
Copy link
Owner

我现在不在机器旁边,估计要下周才能测试这个问题,初步猜测,是不是你用的zlog版本比较旧?原先有版本有这种问题,重复init和fini会有导致内存错误。可以去github上下最新的1.2.10版试试看。
在 2013-7-26 AM9:33,"sh19871122" notifications@github.com写道:

我将程序中的zlog更换为其他日志系统时,没问题,或是程序中不使用log时也没问题,使用zlog的时候每次主程序退出时将段错。我将zlog单独写个简单程序也没问题,我的链接库有pthread
gtest zlog rt pcap,执行代码如下:
29 zlog_category_t *my_log;
30
31 int main(int argc, char *argv[])
32 {
33 if (zlog_init("analysistcp_log.conf"))
34 {
35 if (zlog_init(NULL))
36 return -1;
37 }
38
39 if ((my_log = zlog_get_category("analysistcp_main")) == NULL)
40 {
41 zlog_fini();
42 zlog_init(NULL);
43 my_log = zlog_get_category("analysistcp_main");
44 }
45
46 if (argc != 2)
47 {
48 zlog_error(my_log, "Usage: %s \n", argv[0]);
49 zlog_fini();
50 return 0;
51 }

详细的栈信息如下:
Program terminated with signal 11, Segmentation fault.
#0 __libc_free (mem=0x20) at malloc.c:3709
3709 if (chunk_is_mmapped(p)) /* release mmapped memory. _/
(gdb) bt full
#0 __libc_free (mem=0x20) at malloc.c:3709
ar_ptr =
p =
hook = 0
#1 #1 0x00007f12fb181d0e in
zc_hashtable_del (a_table=0x129d3c0) at zc_hashtable.c:80
i =
p =
q =
#2 #2 0x00007f12fb17c1c6 in
zlog_mdc_del (a_mdc=0x12c1000) at mdc.c:36
No locals.
#3 #3 0x00007f12fb1812fb in
zlog_thread_del (a_thread=0x129d470) at thread.c:45
No locals.
#4 #4 0x00000036584369f1 in
__run_exit_handlers (status=0) at exit.c:78
atfct =
onfct =
cxafct =
#5 #5 exit (status=0) at
exit.c:100
No locals.
#6 #6 0x000000365841ee64 in
_libc_start_main (main=0x41e8c0 <main(int, char*)>, argc=1,
ubp_av=0x7fff4b937d38, init=,
fini=, rtld_fini=, stack_end=0x7fff4b937d28) at libc-start.c:258
result =
unwind_buf = {cancel_jmp_buf = {{jmp_buf = {0, 401847908166741622,
4219392, 140734461345072, 0, 0, -401610168829006218,
432088316090986102}, mask_was_saved = 0}}, priv = {pad = {0x0, 0x0,
0x451e80, 0x7fff4b937d38}, data = {prev = 0x0,
cleanup = 0x0, canceltype = 4529792}}}
not_first_call =
#7 #7 0x0000000000406229 in
_start ()
No symbol table info available.


Reply to this email directly or view it on GitHubhttps://github.com//issues/36
.

@hallelujah-shih
Copy link
Author

直接上代码,如果代码直接构造,然后析构没问题,如果中间随便调用了一个宏之后再析构程序退出的时候将会出现段错,谢谢你抽时间看了问题,非常感谢

/*

  • =====================================================================================
    *
  •   Filename:  test.cpp
    
  • Description:
  •    Version:  1.0
    
  •    Created:  2013年07月26日 15时29分32秒
    
  •   Revision:  none
    
  •   Compiler:  gcc
    
  •     Author:  Hallelujah (shih), sh19871122@gmail.com
    
  •     编译:g++ test.cpp -g -lzlog -lpthread
    
  •     运行:./a.out 
    
  •     当运行不带参数的时候直接段错,当带一个参数的时候正常:./a.out 1
    
  • Organization:
  • =====================================================================================
    */

#include <zlog.h>

zlog_category_t *my_log;

int main(int argc, char *argv[])
{
if (zlog_init("analysistcp_log.conf"))
{
if (zlog_init(NULL))
return -1;
}

if ((my_log = zlog_get_category("analysistcp_main")) == 0)
{
    zlog_fini();
    zlog_init(NULL);
    my_log = zlog_get_category("analysistcp_main");
}

if (argc != 2)
{
    zlog_error(my_log, "Usage:%s\n", argv[0]);
    zlog_fini();

    return -1;
}

// zlog_info(my_log, "Usage:");
zlog_fini();
return 0;

}

@HardySimpson
Copy link
Owner

问题出在:

static void zlog_clean_rest_thread(void)
{
    zlog_thread_t *a_thread;
    a_thread = pthread_getspecific(zlog_thread_key);
    if (!a_thread) return;
    zlog_thread_del(a_thread);
    return;
}

....

zlog_init() { 
....
    pthread_key_create(&zlog_thread_key, (void (*) (void *)) zlog_thread_del);
    rc = atexit(zlog_clean_rest_thread);
}

在zlog_init的时候,我会调用pthread_key_create来注册删除线程的退出函数,让程序在退出线程的时候删掉zlog的线程私有的内存。但问题是,如果用户程序只有一个主线程,这个函数不其作用,主线程的zlog_thread_t变量不会被清理,所以我增加了一行atexit,让程序在退出主线程的时候清理

你的程序反复调用了zlog_init,所以调用了多次atexit,而这个效果会叠加,导致重复去做释放的事情。。

目前我发布了1.2.11,修正了这个bug。

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

No branches or pull requests

2 participants