boost :: program_options给出了malloc错误

时间:2013-01-16 21:07:42

标签: gcc boost malloc macports boost-program-options

我有以下玩具程序,它在OSX 10.6上出现了MacPorts gcc的错误

#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.5升版。我将程序编译为

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

更新:这似乎适用于具有旧版Boost的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上。

1 个答案:

答案 0 :(得分:1)

出错的最可能原因是program_options头文件中显示的接口与编译库中拾取的实现之间存在不匹配。这可能是因为您不小心从不同版本的boost中获取了编译库,或者可能是因为您使用不同版本的编译器将库编译为您用来编译测试程序的库。