我有以下的玩具程序,給出了OSX 10.6的boost :: program_options給出的malloc錯誤
#include <boost/program_options.hpp>
namespace po = boost::program_options;
#include <iostream>
using namespace std;
int main(int ac, char* av[])
{
po::options_description desc("Allowed options");
desc.add_options() ("help", "produce help message") ;
po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
return 0;
}
cout << "Program continues\n";
return 0;
}
我有MacPorts的安裝升壓版本1.52與MacPorts的海灣合作委員會的錯誤。我編譯程序作爲
g++ a.cpp -lboost_program_options-mt -L/opt/local/lib -g -O0
它編譯罰款:
$ ./a.out
Program continues
但它不能打印幫助消息:
$ ./a.out --help
Allowed options:
a.out(40110) malloc: *** error for object 0x7fff70ca3500: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap
我聽說像這樣的東西可能發生,如果使用與用於構建程序的版本不同的gcc版本編譯庫。我如何檢查這個?我有
$ g++ --version
g++ (MacPorts gcc47 4.7.2_2) 4.7.2
更新:這似乎與和老年加速Linux機器上工作。
更新2:GDB的輸出如下
(gdb) run
Starting program: /Users/yasir/Downloads/mask.util/a.out --help
Reading symbols for shared libraries ++++.. done
Allowed options:
a.out(42256) malloc: *** error for object 0x7fff70ca3500: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal SIGABRT, Aborted.
0x00007fff821030b6 in __kill()
(gdb) bt
#0 0x00007fff821030b6 in __kill()
#1 0x00007fff821a39f6 in abort()
#2 0x00007fff820bb195 in free()
#3 0x00000001001188b4 in std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::overflow()
(gdb)
更新3:程序正常工作與Xcode的gcc42,問題只發生與MacPorts的GCC。
這段代碼看起來對我來說是正確的(與我使用program_options的源代碼相比)。你可以在gdb中運行(用-ggdb3 -O0編譯並運行'gdb --args ./a.out --help'並鍵入'run'和'bt')? –
@RafałRawicki,剛剛在編譯後加上了gdb信息 – highBandWidth