我的程序在构造 std 字符串对象时出现死锁。这是 pstack 生成的调用堆栈信息。
Thread 5 (Thread 0x7efffaa02700 (LWP 3012)):
#0 0x0000003742af4e6e in __lll_lock_wait_private () from /lib64/libc.so.6
#1 0x0000003742a7bae8 in _L_lock_9164 () from /lib64/libc.so.6
#2 0x0000003742a79482 in malloc () from /lib64/libc.so.6
#3 0x000000374220cb7b in _dl_map_object_deps () from /lib64/ld-linux-x86-64.so.2
#4 0x0000003742212991 in dl_open_worker () from /lib64/ld-linux-x86-64.so.2
#5 0x000000374220e106 in _dl_catch_error () from /lib64/ld-linux-x86-64.so.2
#6 0x00000037422123ea in _dl_open () from /lib64/ld-linux-x86-64.so.2
#7 0x0000003742b22f80 in do_dlopen () from /lib64/libc.so.6
#8 0x000000374220e106 in _dl_catch_error () from /lib64/ld-linux-x86-64.so.2
#9 0x0000003742b230d7 in __libc_dlopen_mode () from /lib64/libc.so.6
#10 0x0000003742afb675 in init () from /lib64/libc.so.6
#11 0x0000003742e0cac3 in pthread_once () from /lib64/libpthread.so.0
#12 0x0000003742afb774 in backtrace () from /lib64/libc.so.6
#13 0x0000003742a6f7bb in __libc_message () from /lib64/libc.so.6
#14 0x0000003742a750c6 in malloc_printerr () from /lib64/libc.so.6
#15 0x0000003742a78b0c in _int_malloc () from /lib64/libc.so.6
#16 0x0000003742a7948d in malloc () from /lib64/libc.so.6
#17 0x00000037452bd0bd in operator new(unsigned long) () from /usr/lib64/libstdc++.so.6
#18 0x000000374529c3c9 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) () from /usr/lib64/libstdc++.so.6
#19 0x000000374529cde5 in ?? () from /usr/lib64/libstdc++.so.6
#20 0x000000374529cf33 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) () from /usr/lib64/libstdc++.so.6
#21 0x0000000000404642 in vFormat(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, __va_list_tag*) ()
#22 0x0000000000435803 in Logger::Log(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, ...) ()
#23 0x000000000042712d in StatThread::ThreadMain() ()
#24 0x000000000041e71b in BaseThread::StartThreadCallback(void*) ()
#25 0x0000003742e077f1 in start_thread () from /lib64/libpthread.so.0
#26 0x0000003742ae570d in clone () from /lib64/libc.so.6
gdb,gdb -p 3012 生成的信息
#0 0x0000003742af4e6e in __lll_lock_wait_private () from /lib64/libc.so.6
#1 0x0000003742a7bae8 in _L_lock_9164 () from /lib64/libc.so.6
#2 0x0000003742a79482 in malloc () from /lib64/libc.so.6
#3 0x000000374220cb7b in _dl_map_object_deps () from /lib64/ld-linux-x86-64.so.2
#4 0x0000003742212991 in dl_open_worker () from /lib64/ld-linux-x86-64.so.2
#5 0x000000374220e106 in _dl_catch_error () from /lib64/ld-linux-x86-64.so.2
#6 0x00000037422123ea in _dl_open () from /lib64/ld-linux-x86-64.so.2
#7 0x0000003742b22f80 in do_dlopen () from /lib64/libc.so.6
#8 0x000000374220e106 in _dl_catch_error () from /lib64/ld-linux-x86-64.so.2
#9 0x0000003742b230d7 in __libc_dlopen_mode () from /lib64/libc.so.6
#10 0x0000003742afb675 in init () from /lib64/libc.so.6
#11 0x0000003742e0cac3 in pthread_once () from /lib64/libpthread.so.0
#12 0x0000003742afb774 in backtrace () from /lib64/libc.so.6
#13 0x0000003742a6f7bb in __libc_message () from /lib64/libc.so.6
#14 0x0000003742a750c6 in malloc_printerr () from /lib64/libc.so.6
#15 0x0000003742a78b0c in _int_malloc () from /lib64/libc.so.6
#16 0x0000003742a7948d in malloc () from /lib64/libc.so.6
#17 0x00000037452bd0bd in operator new(unsigned long) () from /usr/lib64/libstdc++.so.6
#18 0x000000374529c3c9 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) () from /usr/lib64/libstdc++.so.6
#19 0x000000374529cde5 in ?? () from /usr/lib64/libstdc++.so.6
#20 0x000000374529cf33 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) ()
from /usr/lib64/libstdc++.so.6
#21 0x0000000000404642 in vFormat(std::string, typedef __va_list_tag __va_list_tag *) (fmt="StatThread MsgQueue len:%u", args=0x7efffaa01bb0)
at common.cpp:19
#22 0x0000000000435803 in Logger::Log (level=4, fileName="statthread.cpp", lineNumber=19, functionName="ThreadMain", formatString=
"StatThread MsgQueue len:%u") at logger.cpp:107
#23 0x000000000042712d in StatThread::ThreadMain (this=0xd8c2d0) at statthread.cpp:19
#24 0x000000000041e71b in BaseThread::StartThreadCallback (data=0xd8c2d0) at basethread.h:42
#25 0x0000003742e077f1 in start_thread () from /lib64/libpthread.so.0
#26 0x0000003742ae570d in clone () from /lib64/libc.so.6
函数vFormat在common.cpp中实现,如下:</p>
string vFormat(string fmt, va_list args) {
char *pBuffer = NULL;
if (vasprintf(&pBuffer, STR(fmt), args) == -1) {
o_assert(false);
return "";
}
string result = pBuffer;
free(pBuffer);
return result;
}
common.cpp第19行是:</p>
string result = pBuffer;
看来死锁是由malloc引起的,而malloc是由字符串的构造函数调用的。我该如何处理这个问题,谢谢帮助!