Python与libstdc++的动态库问题

3 投票
2 回答
1738 浏览
提问于 2025-04-17 03:08

总结一下:一个Python模块链接的libstdc++.dylib版本和Python执行文件的版本不一样,导致模块里的iostream调用崩溃。

背景故事

我在一台老电脑上(运行10.5.8)用SWIG创建一个Python模块。由于各种原因,我使用的是GCC 4.5(通过MacPorts安装),而Python 2.7也是通过MacPorts安装的,编译时用的是系统自带的GCC 4.0.1。

观察到的行为

简单来说:在Python中调用str( myObject )时,会导致C++代码调用std::operator<< <std::char_traits<char> >,这时会出现以下错误:

Python(487) malloc: *** error for object 0x69548c: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug

设置断点并在失败时调用backtrace会得到:

#0  0x9734de68 in malloc_error_break ()
#1  0x97348ad0 in szone_error ()
#2  0x97e6fdfc in std::string::_Rep::_M_destroy ()
#3  0x97e71388 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string ()
#4  0x97e6b748 in std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::overflow ()
#5  0x97e6e7a0 in std::basic_streambuf<char, std::char_traits<char> >::xsputn ()
#6  0x00641638 in std::__ostream_insert<char, std::char_traits<char> > ()
#7  0x006418d0 in std::operator<< <std::char_traits<char> > ()
#8  0x01083058 in meshLib::operator<< <tranSupport::Dimension<(unsigned short)1> > (os=@0xbfffc628, c=@0x5a3c50) at /Users/sethrj/_code/pytrt/meshlib/oned/Cell.cpp:21
#9  0x01008b14 in meshLib_Cell_Sl_tranSupport_Dimension_Sl_1u_Sg__Sg____str__ (self=0x5a3c50) at /Users/sethrj/_code/_build/pytrt-gcc45DEBUG/meshlib/swig/mesh_onedPYTHON_wrap.cxx:4439
#10 0x0101d150 in _wrap_Cell_T___str__ (args=0x17eb470) at /Users/sethrj/_code/_build/pytrt-gcc45DEBUG/meshlib/swig/mesh_onedPYTHON_wrap.cxx:8341
#11 0x002f2350 in PyEval_EvalFrameEx ()
#12 0x002f4bb4 in PyEval_EvalCodeEx ()
[snip]

怀疑的问题

我认为问题在于我的代码链接了一个新版本的libstdc++:

/opt/local/lib/gcc45/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.14.0)

而Python的二进制文件则间接依赖于系统的libstdc++,这个库先被加载(在gdb中用info shared输出的结果):

  1 dyld                  - 0x8fe00000        dyld Y Y /usr/lib/dyld at 0x8fe00000 (offset 0x0) with prefix "__dyld_"
  2 Python                - 0x1000            exec Y Y /opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python (offset 0x0)
                                          (objfile is) /opt/local/bin/python
  3 Python                F 0x219000          dyld Y Y /opt/local/Library/Frameworks/Python.framework/Versions/2.7/Python at 0x219000 (offset 0x219000)
  4 libSystem.B.dylib     - 0x9723d000        dyld Y Y /usr/lib/libSystem.B.dylib at 0x9723d000 (offset -0x68dc3000)
                                 (commpage objfile is) /usr/lib/libSystem.B.dylib[LC_SEGMENT.__DATA.__commpage]
  5 CoreFoundation        F 0x970b3000        dyld Y Y /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation at 0x970b3000 (offset -0x68f4d000)
  6 libgcc_s.1.dylib      - 0x923e6000        dyld Y Y /usr/lib/libgcc_s.1.dylib at 0x923e6000 (offset -0x6dc1a000)
  7 libmathCommon.A.dylib - 0x94af5000        dyld Y Y /usr/lib/system/libmathCommon.A.dylib at 0x94af5000 (offset -0x6b50b000)
  8 libicucore.A.dylib    - 0x97cf4000        dyld Y Y /usr/lib/libicucore.A.dylib at 0x97cf4000 (offset -0x6830c000)
  9 libobjc.A.dylib       - 0x926f0000        dyld Y Y /usr/lib/libobjc.A.dylib at 0x926f0000 (offset -0x6d910000)
                                 (commpage objfile is) /usr/lib/libobjc.A.dylib[LC_SEGMENT.__DATA.__commpage]
 10 libauto.dylib         - 0x95eac000        dyld Y Y /usr/lib/libauto.dylib at 0x95eac000 (offset -0x6a154000)
 11 libstdc++.6.0.4.dylib - 0x97e3d000        dyld Y Y /usr/lib/libstdc++.6.0.4.dylib at 0x97e3d000 (offset -0x681c3000)
 12 _mesh_oned.so         - 0x1000000         dyld Y Y /Users/sethrj/_code/_build/pytrt-gcc45DEBUG/meshlib/swig/_mesh_oned.so at 0x1000000 (offset 0x1000000)
 13 libhdf5.7.dylib       - 0x122c000         dyld Y Y /opt/local/lib/libhdf5.7.dylib at 0x122c000 (offset 0x122c000)
 14 libz.1.2.5.dylib      - 0x133000          dyld Y Y /opt/local/lib/libz.1.2.5.dylib at 0x133000 (offset 0x133000)
 15 libstdc++.6.dylib     - 0x600000          dyld Y Y /opt/local/lib/gcc45/libstdc++.6.dylib at 0x600000 (offset 0x600000)
[snip]

注意,malloc错误发生在系统libstdc++的内存地址上,而不是共享库链接的那个。

尝试的解决方案

我试图强制MacPorts使用GCC 4.5来构建Python,而不是使用苹果的编译器,但安装阶段失败了,因为它需要创建一个Mac的“框架”,而普通的GCC显然做不到这一点。

即使使用了-static-libstdc++这个编译选项,__ostream_insert仍然调用了系统加载的共享库中的std::basic_streambuf

我尝试通过在前面加上/opt/local/lib/gcc45/来修改DYLD_LIBRARY_PATH,但没有效果。

我该怎么做才能让这个工作?我真的快要绝望了。

更多信息

这个问题似乎在mac os x上很常见。注意在所有调试输出中,地址在std::__ostream_insertstd::basic_streambuf::xsputn之间跳动:它在新版本的GCC 4.5代码和旧的共享库代码(在/usr/bin中)之间切换。现在,得想个解决办法……

2 个回答

-3

在GDB中运行Python,并在malloc_error_break上设置一个断点。这样可以让你看到哪些东西被释放了,但其实并没有分配过。我觉得这不是因为libstdc++的不同版本之间的ABI(应用程序二进制接口)出错。

2

解决了这个问题。我发现这个问题在Mac上混用不同版本的GCC时并不少见。在阅读了关于mpich的解决方案并查看了mpich的源代码后,我发现解决办法是在Mac系统的gcc中添加以下标志:

-flat_namespace

我太开心了。真希望我不需要花一周的时间才搞明白这个问题。:)

撰写回答