Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::program_options gives malloc error

I have the following toy program that gives errors with the 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 boost version 1.52 installed 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 it 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've heard that stuff like this can happen if the libraries are compiled with a different version of gcc than the one used to build the program. How do I check for 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 and older Boost.

Update 2: the output of gdb 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 the Xcode gcc42, the problem only occurs with the MacPorts gcc.

like image 540
highBandWidth Avatar asked Jan 16 '13 21:01

highBandWidth


1 Answers

The most likely reason for your error is that there is a mismatch between the interfaces presented in the program_options header files and the implementation as picked up in your compiled library. This could be either because you are accidentally picking up a compiled library from a different version of boost or perhaps because you compiled the library with different version of the compiler to the one you are using to compile your test program.

like image 186
Bojan Nikolic Avatar answered Nov 08 '22 02:11

Bojan Nikolic