1

我正在测试我的多线程服务器的 Mac OS X 端口。它启动了,但是在工作线程接受第一个客户端请求后不久,它就在 vsnprintf 中死掉了。

似乎 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 中的错误有关。请参阅c++ 中 int 的 ostringstream 问题

4

1 回答 1

0

Mac OS X 不使用单独的libpthread. 所有 pthread 函数都在libSystem

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

_GLICXX_DEBUG=1如您的编辑中链接的问题所建议的那样,在没有定义的情况下进行编译是否可以解决问题?

于 2011-07-02T17:04:00.147 回答