Boost :: program_options gives malloc error

I have the following toy program that gives errors with MacPorts gcc on OSX 10.6

#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; } 

I have version 1.52 version with MacPorts. I compile the program as

 g++ a.cpp -lboost_program_options-mt -L/opt/local/lib -g -O0 

It compiles fine:

 $ ./a.out Program continues 

But he cannot print the help message:

 $ ./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 

I have heard that such things can happen if the libraries are compiled with a different version of gcc than the one used to build the program. How can I check this? I have

 $ g++ --version g++ (MacPorts gcc47 4.7.2_2) 4.7.2 

Update : this seems to work on a Linux machine with an older Boost.

Update 2 : gdb output follows

 (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) 

Update 3 . The program works fine with Xcode gcc42, the problem only occurs with MacPorts gcc.

+6
source share
1 answer

The most likely cause of your error is a mismatch between the interfaces represented in the program_options header files and the implementation selected in your compilation library. This may be due to the fact that you accidentally compiled a compiled library from another version of boost or, perhaps, because you compiled the library with a different version of the compiler to the one you use to compile your test program.

+1
source

All Articles