多线程 list没有锁好导致的 不安全宕机问题

来源:互联网 发布:淘宝售后针对评价话术 编辑:程序博客网 时间:2024/06/05 03:57


 


问题:因为代码中执行m_LoggerList.size()语句没有加锁,导致内存中出现了空指针,以下为状态推送线程的堆栈信息:

(gdb) thread 20
[Switching to thread 20 (Thread 4884)]#0  std::_List_const_iterator<_st_LOGGER*>::operator++() (this=0x20c6b498)
    at /opt/freeware/lib/gcc/powerpc-ibm-aix5.3.0.0/4.2.0/include/c++/bits/stl_list.h:224
224             _M_node = _M_node->_M_next;
(gdb) bt
#0  std::_List_const_iterator<_st_LOGGER*>::operator++() (this=0x20c6b498)
    at /opt/freeware/lib/gcc/powerpc-ibm-aix5.3.0.0/4.2.0/include/c++/bits/stl_list.h:224
#1  0x10014d2c in std::iterator_traits<std::_List_const_iterator<_st_LOGGER*> >::difference_type std::__distance<std::_List_const_iterator<_st_LOGGER*> >(std::_List_const_iterator<_st_LOGGER*>, std::_List_const_iterator<_st_LOGGER*>, std::input_iterator_tag) (__first=
      {_M_node = 0x0}, __last={_M_node = 0x20031b70})
    at /opt/freeware/lib/gcc/powerpc-ibm-aix5.3.0.0/4.2.0/include/c++/bits/stl_iterator_base_funcs.h:84
#2  0x10014e60 in std::iterator_traits<std::_List_const_iterator<_st_LOGGER*> >::difference_type std::distance<std::_List_const_iterator<_st_LOGGER*> >(std::_List_const_iterator<_st_LOGGER*>, std::_List_const_iterator<_st_LOGGER*>) (__first={_M_node = 0x20e51b08},
    __last={_M_node = 0x20031b70})
    at /opt/freeware/lib/gcc/powerpc-ibm-aix5.3.0.0/4.2.0/include/c++/bits/stl_iterator_base_funcs.h:119
#3  0x10014f50 in std::list<_st_LOGGER*, std::allocator<_st_LOGGER*> >::size() const (this=0x20031b70)
    at /opt/freeware/lib/gcc/powerpc-ibm-aix5.3.0.0/4.2.0/include/c++/bits/stl_list.h:660
#4  0x100172e4 in nsLogApi::CLogger::wlog2(PRIORITY_TYPE, char const*, char const*, char const*, char const*, char const*) (
    this=0x2002f518, enumLevel=PRIORITY_DEBUG, sSysID=0x200011f8 "Ag\ent", sLocation=0x20001378 "./Stat\eCh\eckThr\ead.cpp 201",
    sErrCode=0x20001578 "0", sDigest=0x10045044 "cli\ent线程池繁忙", sLogMsg=0x20019be4 "1234455")
    at ../public/p_logapi/src/logger.cpp:120
#5  0x1003ad4c in CStateCheckThread::Run() (this=0x200733f8) at ./StateCheckThread.cpp:201
#6  0x10035f70 in _ThreadEntry(void*) (pParam=0x200733f8) at ../public/p_frame/src/Thread.cpp:23
#7  0xd010d784 in _pthread_body () from /usr/lib/libpthreads.a(shr_xpg5.o)
(gdb) frame 0
#0  std::_List_const_iterator<_st_LOGGER*>::operator++() (this=0x20c6b498)
    at /opt/freeware/lib/gcc/powerpc-ibm-aix5.3.0.0/4.2.0/include/c++/bits/stl_list.h:224
224             _M_node = _M_node->_M_next;
(gdb) info f
Stack level 0, frame at 0x20c6b3f0:
 pc = 0x10014c80 in std::_List_const_iterator<_st_LOGGER*>::operator++()
    (/opt/freeware/lib/gcc/powerpc-ibm-aix5.3.0.0/4.2.0/include/c++/bits/stl_list.h:224); saved pc 0x10014d2c
 called by frame at 0x20c6b420
 source language c++.
 Arglist at 0x20c6b3f0, args: this=0x20c6b498
 Locals at 0x20c6b3f0, Previous frame's sp in r1
 Saved registers:
  r31 at 0x20c6b41c
(gdb) up
#1  0x10014d2c in std::iterator_traits<std::_List_const_iterator<_st_LOGGER*> >::difference_type std::__distance<std::_List_const_iterator<_st_LOGGER*> >(std::_List_const_iterator<_st_LOGGER*>, std::_List_const_iterator<_st_LOGGER*>, std::input_iterator_tag) (__first=
      {_M_node = 0x0}, __last={_M_node = 0x20031b70})
    at /opt/freeware/lib/gcc/powerpc-ibm-aix5.3.0.0/4.2.0/include/c++/bits/stl_iterator_base_funcs.h:84
84                ++__first;
(gdb) info f
Stack level 1, frame at 0x20c6b420:
 pc = 0x10014d2c
    in std::iterator_traits<std::_List_const_iterator<_st_LOGGER*> >::difference_type std::__distance<std::_List_const_iterator<_st_LOGGER*> >(std::_List_const_iterator<_st_LOGGER*>, std::_List_const_iterator<_st_LOGGER*>, std::input_iterator_tag)
    (/opt/freeware/lib/gcc/powerpc-ibm-aix5.3.0.0/4.2.0/include/c++/bits/stl_iterator_base_funcs.h:84); saved pc 0x10014e60
 called by frame at 0x20c6b480, caller of frame at 0x20c6b3f0
 source language c++.
 Arglist at 0x20c6b420, args: __first={_M_node = 0x0}, __last={_M_node = 0x20031b70}
 Locals at 0x20c6b420, Previous frame's sp in r1
 Saved registers:
  r31 at 0x20c6b47c, lr at 0x20c6b488
(gdb) up
(gdb) up
(gdb) up
#4  0x100172e4 in nsLogApi::CLogger::wlog2(PRIORITY_TYPE, char const*, char const*, char const*, char const*, char const*) (
    this=0x2002f518, enumLevel=PRIORITY_DEBUG, sSysID=0x200011f8 "Ag\ent", sLocation=0x20001378 "./Stat\eCh\eckThr\ead.cpp 201",
    sErrCode=0x20001578 "0", sDigest=0x10045044 "cli\ent线程池繁忙", sLogMsg=0x20019be4 "1234455")
    at ../public/p_logapi/src/logger.cpp:120
120             if(m_LogConfig.m_iAsynLength > 0 && m_LoggerList.size() > m_LogConfig.m_iAsynLength)
(gdb) p m_LogConfig.m_iAsynLength
$1 = 8000
(gdb) p  m_LoggerList.size()
aix-thread: ptrace (57, 0) returned -1 (errno = 3 The process does not exist.)

由以上堆栈信息可以看出,在执行m_LoggerList.size()语句时出现了__first={_M_node = 0x0}的空地址, 导致程序在执行_M_node = _M_node->_M_next; 时出错。

解决

m_LogMutex.Lock();
if(m_LogConfig.m_iAsynLength > 0 && m_LoggerList.size() > m_LogConfig.m_iAsynLength){
printf("m_LoggerList Length:[%d]\n",m_LoggerList.size());
m_LogMutex.UnLock();
return;
}//在这里加上锁
m_LogMutex.UnLock();


 

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小金鱼翻肚皮了怎么办 秋天树叶没了小鸟怎么办 去国外旅游不会英语怎么办 橡皮树长了2米高怎么办 榕树盆景长的高怎么办? 2岁宝宝看书弯腰低头怎么办 excel表格打开很慢怎么办 3d模型有红线框怎么办 电视页面加载时错误怎么办 投屏显示加载视频错误怎么办 word遇到问题需要关闭怎么办 画眼线看不出来怎么办 14岁眼皮很松怎么办啊 ps存不了psd格式怎么办 花草上有白色物怎么办 ps抠出来有白边头发怎么办 脸上结痂掉了有红印怎么办 海棠花瓣干枯怎么办茎变软 微信上的图片打不开怎么办 口红吊兰老掉叶子怎么办 翠叶竹芋叶子卷怎么办 牙有龋齿垫底以后酸怎么办 事业单位辞职请示30天不批怎么办 孕早期吃了油菜怎么办 实体店买到翻新苹果手机怎么办 太阳花叶子蔫了怎么办 刚摘下来的多肉怎么办 购车4s不给合同怎么办 橙光游戏2.0商城怎么办 飞羽花卷叶了怎么办 孔雀竹芋叶子发黄卷曲怎么办 孔雀竹芋叶子黄了卷了怎么办 飞羽竹芋叶尖黄怎么办 十七岁还是胸小怎么办 英雄联盟连接不上服务器怎么办 彩叶草叶子蔫了怎么办 家里种葱老是死怎么办 非洲菊生虫子了怎么办 多肉的花剪下来怎么办 结石痛怎么办怎么止疼 喝玫瑰花茶胃疼怎么办