我在使用静态boost库时遇到了一些非常奇怪的问题(来自MacPorts的Boost 1.45.0-2,编译为fat/universal(x86/x86_64)库),在Mac OS X 10.6.6下使用GCC 4.5。
错误消息为:
这个问题通常在将静态boost库链接到二进制文件时出现。尽管动态链接可以正常工作。
更多信息:
测试/使用的gcc版本:Apple GCC 4.2.1(可用/运行),MacPorts GCC 4.5.2(失败)
测试/使用的标志:无,-fPIC,-fPIC -g,-fPIC -g -ggdb3 -gdwarf-2 -O0
使用MP GCC 4.5.2 / 上述任何标志的gdb输出:
错误消息为:
main(78485) malloc: *** error for object 0x1000e0b20: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
[1] 78485 abort (core dumped)
以下是一个微小的示例代码,将会触发此问题:
#define BOOST_FILESYSTEM_VERSION 3
#include <boost/filesystem.hpp>
#include <iostream>
int main (int argc, char **argv) {
std::cout << boost::filesystem::current_path ().string () << '\n';
}
这个问题通常在将静态boost库链接到二进制文件时出现。尽管动态链接可以正常工作。
更多信息:
测试/使用的gcc版本:Apple GCC 4.2.1(可用/运行),MacPorts GCC 4.5.2(失败)
测试/使用的标志:无,-fPIC,-fPIC -g,-fPIC -g -ggdb3 -gdwarf-2 -O0
使用MP GCC 4.5.2 / 上述任何标志的gdb输出:
(gdb) run
Starting program: /Users/ionic/crashtest/bin/ctest Reading symbols for shared libraries .++++++++++++++++++++++.................................................................................................................. done
ctest(80366) malloc: *** error for object 0x100fe6b20: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal SIGABRT, Aborted. 0x00007fff81a4e616 in __kill ()
(gdb) bt full
#0 0x00007fff81a4e616 in __kill () No symbol table info available.
#1 0x00007fff81aeecca in abort () No symbol table info available.
#2 0x00007fff81a066f5 in free () No symbol table info available.
#3 0x0000000100f763e9 in std::string::_M_mutate () No symbol table info available.
#4 0x0000000100f7644c in std::string::_M_replace_safe () No symbol table info available.
#5 0x0000000100f77edd in std::string::replace () No symbol table info available.
#6 0x000000010000713d in std::string::_M_rep () at /usr/include/c++/4.2.1/bits/basic_string.h:1412
to = (string &) Cannot access memory at address 0x0
看起来使用苹果(相当老的)GCC版本运行良好,但是使用MacPorts构建的新GCC版本则出现了严重问题。
otool -L ctest:
./../../bin/ctest:
/opt/local/lib/gcc45/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.14.0)
/usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 625.0.0)
/opt/local/lib/gcc45/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 125.2.1)
我曾经看到过很多关于OS X的报告,涉及GCC 4.2和_GLIBCXX_DEBUG宏设置的类似bug,但是这个问题似乎更普遍,因为我既没有使用XCode,也没有设置宏(即使取消定义也没用。我只是想确保它与此问题确实没有关系。)这似乎与这个问题完全无关,因为相同的代码在Apple的GCC上运行良好。
由于Apple的GCC尚未包含任何C++0x功能,因此我确实希望使用当前稳定的GCC版本。
是否有人知道为什么会发生这种情况,甚至可能有解决方案(而不是使用动态库解决方法)?
最好的问候,
Mihai