Opened 7 years ago

Last modified 7 years ago

#12017 new Bugs

Segfault with Boost program_options 1.60 debug libraries on OSX

Reported by: anonymous Owned by: Vladimir Prus
Milestone: To Be Determined Component: program_options
Version: Boost 1.60.0 Severity: Problem
Keywords: Cc:

Description

Hi,

I'm using Boost 1.60.0_1 (installed from Brew) and am trying to compile the following file:

//mytest.cpp:
#include <boost/program_options.hpp>
#include <string>

namespace po = boost::program_options;

int main(int argc, char *argv[])
{
    po::variables_map vm;
    po::options_description desc;

    desc.add_options()
    ("test,t", po::value<std::string>()->default_value("something"))
    ;

    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);
    return 0;
}

I'm using CMake to compile. When compiling in Release mode, everything works fine. But when compiling in Debug mode, i get a segfault in the store() function when I run the program without the -t option:

(lldb) bt
* thread #1: tid = 0x5f2ad5, 0x000000010001b9c3 mytest`boost::program_options::value_semantic_codecvt_helper<char>::parse(boost::any&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, bool) const [inlined] std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::size(this=0xffffffffffffffd5 size=0) const at vector:653, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0xffffffffffffffd5)
  * frame #0: 0x000000010001b9c3 mytest`boost::program_options::value_semantic_codecvt_helper<char>::parse(boost::any&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, bool) const [inlined] std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::size(this=0xffffffffffffffd5 size=0) const at vector:653
    frame #1: 0x000000010001b9c3 mytest`boost::program_options::value_semantic_codecvt_helper<char>::parse(this=0x0000000100103740, value_store=0x00007fff5fbff408, new_tokens=size=0, utf8=<unavailable>) const + 51 at value_semantic.cpp:45
    frame #2: 0x000000010001934d mytest`boost::program_options::store(options=0x00007fff5fbff5c8, xm=0x00007fff5fbff6c8, utf8=<unavailable>) + 1207 at variables_map.cpp:125
    frame #3: 0x0000000100000cad mytest`main + 445
    frame #4: 0x00007fff92a5e5ad libdyld.dylib`start + 1

I noticed this happens only if the binary is linked with the static debug libraries, i.e libboost_program_options-d.a and libboost_program_options-mt-d.a. To reproduce without CMake:

$  c++ mytest.cpp -I/usr/local/include /usr/local/lib/libboost_program_options-d.a -o mytest && ./mytest
[1]    83707 segmentation fault  ./mytest

$  c++ mytest.cpp -DDEBUG -I/usr/local/include /usr/local/lib/libboost_program_options-d.a -o mytest && ./mytest
[1]    83709 segmentation fault  ./mytest

It crashes only when run without the -t argument; If I provide it, it works fine:

$ ./mytest -t test

It does not crash when linked statically with a non-debug library:

$ c++ mytest.cpp -DDEBUG -I/usr/local/include /usr/local/lib/libboost_program_options.a -o mytest && ./mytest
$ c++ mytest.cpp -DDEBUG -I/usr/local/include /usr/local/lib/libboost_program_options-mt.a -o mytest && ./mytest

Or when linked dynamically with one of the shared libraries:

$ c++ mytest.cpp -I/usr/local/include /usr/local/opt/boost/lib/libboost_program_options.dylib -o mytest && ./mytest
$ c++ mytest.cpp -I/usr/local/include /usr/local/opt/boost/lib/libboost_program_options-mt.dylib -o mytest && ./mytest

The only workaround I found in order to compile a working debug binary with CMake is to use -DBoost_USE_STATIC_RUNTIME=ON because for some reason it tells the compiler to link with the shared library).

Change History (2)

comment:1 by anonymous, 7 years ago

Component: Noneprogram_options
Owner: set to Vladimir Prus

comment:2 by dimansp@…, 7 years ago

The same story when i'm using program_options 1_60 on Red Hat 4.1.2-44 (kernel version: 2.6.18-128.el5). But program_options 1_58 works perfect.

I'm using the following command for compile boost:

./b2 --with-program_options variant=release runtime-link=static link=static --stagedir=.. stage
./b2 --with-program_options variant=debug runtime-link=static link=static --stagedir=.. stage
Note: See TracTickets for help on using tickets.