2009-11-06 61 views
1

我正在测试多线程服务器的Mac OS X端口。它会启动,但在工作线程第一次请求客户端请求后不久,它会在vsnprintf中死掉。Mac OS X端口在glibstdC++ vsnprintf中的pthread_setspecific中崩溃 - 如何排除故障?

看来,vsnprintf正试图用pthread_setspecific来操纵一些线程本地内存。这引用了一个糟糕的指针。 然后,gdb捕获一个dlopen调用,得到一个错误,并且试图格式化它自己的错误信息。 因为要格式化错误,它需要设置一些线程本地内存!

在此之前,我自己的代码成功地使用了pthread_create_key,pthread_getspecific和pthread_setspecific。我仔细记录了自己的访问权限,我不认为他们正在破坏任何内容。

glibstdC++中的某些静态可能没有按时初始化吗?我怎么知道?

此外,我使用g ++ -pthread编译和链接,但在可执行文件中看不到libpthread。

otool -L myExecutable 

libboost_thread-xgcc40-mt-1_39.dylib (compatibility version 0.0.0, current version 0.0.0) 
/Users/eolson//lib/libgsl.0.dylib (compatibility version 14.0.0, current version 14.0.0) 
/Users/eolson//lib/libgslcblas.0.dylib (compatibility version 1.0.0, current version 1.0.0) 
/Users/eolson/mico-2.3.12/lib/libmico2.3.12.dylib (compatibility version 0.0.0, current version 0.0.0) 
/usr/local/lib/libModelsCorba.dylib (compatibility version 1.0.0, current version 1.0.0) 
/usr/local/lib/libModelsBigLibrary.dylib (compatibility version 1.0.0, current version 1.0.0) 
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.4.0) 
/usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0) 
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.4) 

有没有人有一个想法如何进一步调试?

堆栈跟踪:触发崩溃

[Switching to process 37784] 
Program received signal: “EXC_BAD_ACCESS”. 
[Switching to process 37784] 
sharedlibrary apply-load-rules all 
Data Formatters temporarily unavailable, will re-try after a 'continue'. (The program being debugged was signaled while in a function called from GDB. 
GDB remains in the frame where the signal was received. 
To change this behavior use "set unwindonsignal on" 
Evaluation of the expression containing the function (dlopen) will be abandoned.) 
(gdb) where 

#0 0x9232f03b in pthread_setspecific() 
#1 0x9232efe6 in getPerThreadBufferFor_dlerror() 
#2 0x8fe0b0cd in __dyld_dlopen() 
#3 0x9232ef48 in dlopen() 
#4 <function called from gdb> 
#5 0x9232f03b in pthread_setspecific() 
#6 0x9233ed64 in __Balloc_D2A() 
#7 0x9233eb92 in __d2b_D2A() 
#8 0x9233dc5e in __dtoa() 
#9 0x92335975 in __vfprintf() 
#10 0x92355886 in vsnprintf() 
#11 0x96eb526b in std::__convert_from_v() 
#12 0x96eaeb5e in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_float<double>() 
#13 0x96eaedb4 in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put() 
#14 0x96ea9583 in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::put() 
#15 0x96eb79dd in std::ostream::_M_insert<double>() 
#16 0x012db6a8 in MyCode ... 

代码:

std::ostringstream buf; 
buf << myObjectWithOutputOperator << endl; 
double x = 1; 
buf << "x: " << x << endl; // crashes during __vfprintf 

编辑:我相信这是关系到用的XCode 3.2 DEBUG配置ostringstream的bug。请参阅ostringstream problem with int in c++

+1

x是否真的没有设置为一个值? – Mark 2009-11-06 23:51:29

+0

对不起,我正在简化。是的,在撞车前x = 1。如果x未初始化,printf将会有问题!另外,代码已经在Linux上部署了一段时间,所以我正在寻找Mac OS X特有的缺陷。 – 2009-11-09 15:17:22

回答

0

Mac OS X不使用单独的libpthread。所有的并行线程功能都在libSystem

$ nm -g /usr/lib/libSystem.dylib | grep ' _pthread_' | wc -l 
    113 

是否编译没有定义_GLICXX_DEBUG=1解决这个问题,在您的编辑链接问题的建议?