找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4160|回复: 3

如何确保每个线程的ACE_Log_Msg释放

[复制链接]
发表于 2009-6-15 21:23:17 | 显示全部楼层 |阅读模式
看了ace有关os_thread的代码,似乎ace把自己管理的程序结束时要释放的都通过ACE_TSS_Cleanup::insert加入到了一个table中。
然后程序结束时,通过ACE_TSS_Cleanup::thread_exit取出来,按照存的时候的指针及其对应释放方法进行释放。
看不懂每个线程的ACE_Log_Msg是怎么管理、控制释放的。
linux下,目前总是在valgrind查泄漏的时候报出很多log泄露。
例如
==31994==    at 0x40051B3: operator new[](unsigned, std::nothrow_t const&) (vg_replace_malloc.c:207)
==31994==    by 0x41A3404: ACE_Log_Msg::ACE_Log_Msg() (Log_Msg.cpp:708)
==31994==    by 0x41A3669: ACE_Log_Msg::instance() (Log_Msg.cpp:365)
==31994==    by 0x41C5E4F: ACE_Object_Manager::init() (Object_Manager.cpp:262)
==31994==    by 0x41C5F94: ACE_Object_Manager::ACE_Object_Manager() (Object_Manager.cpp:312)
==31994==    by 0x41C605A: ACE_Object_Manager::instance() (Object_Manager.cpp:332)
==31994==    by 0x41C60E0: ACE_Object_Manager_Manager::ACE_Object_Manager_Manager() (Object_Manager.cpp:757)
==31994==    by 0x41C6125: __static_initialization_and_destruction_0(int, int) (Object_Manager.cpp:773)
==31994==    by 0x421D9E5: (within /home/iv3/lib/libACE-5.6.1.so)
==31994==    by 0x4158F2C: (within /home/iv3/lib/libACE-5.6.1.so)
==31994==    by 0x9CBF02: call_init (in /lib/ld-2.5.so)
==31994==    by 0x9CC012: _dl_init (in /lib/ld-2.5.so)
==31994==    by 0x9BE84E: (within /lib/ld-2.5.so)

另外,valgrind这个分析结果是程序正常运行推出后生成的,其中关于ace的log有时是确定的泄露,有时是扔在使用中的内存。
发表于 2009-6-15 22:26:54 | 显示全部楼层
判断是否是一次性的问题。如果是一次性的问题,可以先pass
有些设计有特殊的目的,往往很难避免一次性的内存泄漏。
 楼主| 发表于 2009-6-16 10:27:12 | 显示全部楼层

回复 #2 winston 的帖子

查了下一次性泄露:
发生内存泄漏的代码只会被执行一次,或者由于算法上的缺陷,导致总会有一块仅且一块内存发生泄漏。比如,在类的构造函数中分配内存,在析构函数中却没有释放该内存,但是因为这个类是一个Singleton,所以内存泄漏只会发生一次


我现在让程序正常退出,连一次性泄露也在退出时也应处理掉,看看有没有用ace不妥的方法导致在线程退出前一些东西没有处理。
ace每个线程都对应个log实例,怕线程退出前要是有没有处理好的log释放,那么怕来回来去开线程会有很多泄露了。
(p.s.我这个线程池是10秒钟没有任务,挂起的线程就退出结束,之后有任务再新开线程。)

最新正常退出的泄露分析:
==13817== 16,546 (8,352 direct, 8,194 indirect) bytes in 2 blocks are definitely lost in loss record 22 of 22
==13817==    at 0x4005311: operator new(unsigned, std::nothrow_t const&) (vg_replace_malloc.c:179)
==13817==    by 0x41A3659: ACE_Log_Msg::instance() (Log_Msg.cpp:365)
==13817==    by 0x41A3701: ACE_Log_Msg::inherit_hook(ACE_OS_Thread_Descriptor*, ACE_OS_Log_Msg_Attributes&) (Log_Msg.cpp:2665)
==13817==    by 0x417526F: ACE_Base_Thread_Adapter::inherit_log_msg() (Base_Thread_Adapter.cpp:65)
==13817==    by 0x420FE74: ACE_Thread_Adapter::invoke() (Thread_Adapter.cpp:53)
==13817==    by 0x4175320: ace_thread_adapter (Base_Thread_Adapter.cpp:116)
==13817==    by 0x43ED2DA: start_thread (in /lib/libpthread-2.5.so)
==13817==    by 0x437814D: clone (in /lib/libc-2.5.so)
 楼主| 发表于 2009-6-16 10:34:41 | 显示全部楼层
一次性内存泄露上面说的,比如ace的托管singleton,在初了main函数ace也管释放,非托管singleton我在调用ace的fini之前也手动close了。总之走正常退出流程也杜绝这样的一次性泄露。


如果不正常退出,也就是运行了很久ctrl+c退出就会有很多reachable的log。
==1279== 962,795 bytes in 235 blocks are still reachable in loss record 101 of 102
==1279==    at 0x40051B3: operator new[](unsigned, std::nothrow_t const&) (vg_replace_malloc.c:207)
==1279==    by 0x41A3404: ACE_Log_Msg::ACE_Log_Msg() (Log_Msg.cpp:708)
==1279==    by 0x41A3669: ACE_Log_Msg::instance() (Log_Msg.cpp:365)
==1279==    by 0x41A3701: ACE_Log_Msg::inherit_hook(ACE_OS_Thread_Descriptor*, ACE_OS_Log_Msg_Attributes&) (Log_Msg.cpp:2665)
==1279==    by 0x417526F: ACE_Base_Thread_Adapter::inherit_log_msg() (Base_Thread_Adapter.cpp:65)
==1279==    by 0x420FE74: ACE_Thread_Adapter::invoke() (Thread_Adapter.cpp:53)
==1279==    by 0x4175320: ace_thread_adapter (Base_Thread_Adapter.cpp:116)
==1279==    by 0x43ED2DA: start_thread (in /lib/libpthread-2.5.so)
==1279==    by 0x437814D: clone (in /lib/libc-2.5.so)
==1279==
==1279==
==1279== 981,360 bytes in 235 blocks are still reachable in loss record 102 of 102
==1279==    at 0x4005311: operator new(unsigned, std::nothrow_t const&) (vg_replace_malloc.c:179)
==1279==    by 0x41A3659: ACE_Log_Msg::instance() (Log_Msg.cpp:365)
==1279==    by 0x41A3701: ACE_Log_Msg::inherit_hook(ACE_OS_Thread_Descriptor*, ACE_OS_Log_Msg_Attributes&) (Log_Msg.cpp:2665)
==1279==    by 0x417526F: ACE_Base_Thread_Adapter::inherit_log_msg() (Base_Thread_Adapter.cpp:65)
==1279==    by 0x420FE74: ACE_Thread_Adapter::invoke() (Thread_Adapter.cpp:53)
==1279==    by 0x4175320: ace_thread_adapter (Base_Thread_Adapter.cpp:116)
==1279==    by 0x43ED2DA: start_thread (in /lib/libpthread-2.5.so)
==1279==    by 0x437814D: clone (in /lib/libc-2.5.so)

[ 本帖最后由 sokiiya 于 2009-6-16 15:16 编辑 ]
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-12-23 17:30 , Processed in 0.017192 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表