segmentation fault using boost::program_options on Linux

Discussion in 'C++' started by cesco, Mar 3, 2006.

  1. cesco

    cesco Guest

    Hi,

    I'm using the boost library program_options for parsing the command
    line given to my program as described in the class Parameter:

    // Main.cpp
    #include "Parameter.h"
    using namespace std;

    int main(int ac, char* av[])
    {
    // Parameter Initialization
    Parameter::Initialize(ac, av);
    }
    // ----------------------------------------


    // Parameter.h
    // #ifndef __PARAMETER_H
    #define __PARAMETER_H

    #include <vector>
    #include <string>
    #include <iostream>
    using namespace std;

    class Parameter
    {
    public:
    static void Initialize(int ac, char* av[])
    {
    if ( mpParameter == NULL ) {
    mpParameter = new Parameter();
    // Parse the command line
    mpParameter->parseCommandLine(ac, av);
    }
    }
    static Parameter* GetParameter()
    {
    if ( mpParameter == NULL ) {
    cout << "Parameter class not initialized correctly" << endl;
    exit(EXIT_FAILURE);
    }
    return mpParameter;
    }
    void parseFiles();
    void parseCommandLine(int ac, char* av[]);
    ~Parameter();
    private:
    // Members
    string mOutputFolder;
    string mDataFolder;
    string mParameterfile;
    static Parameter* mpParameter;
    Parameter();
    };

    #endif // __PARAMETER_H
    // -----------------------------------------------

    // Parameter.cpp
    #include <iostream>
    #include <iterator>
    #include "Parameter.h"
    #include <boost/program_options.hpp>
    namespace po = boost::program_options;

    Parameter* Parameter::mpParameter = NULL;

    void Parameter::parseCommandLine(int ac, char* av[])
    {
    try {
    po::eek:ptions_description desc("Allowed options");
    desc.add_options()
    ("help", "Show this help message")
    ("parameterfile,p", po::value<string>(&mParameterfile),
    "Set Outputfolder");

    po::variables_map vm;
    po::store(po::parse_command_line(ac, av, desc), vm);
    po::notify(vm);

    if (vm.count("help")) {
    cout << desc << "\n";
    }

    if (vm.count("parameterfile")) {
    cout << "Parameterfile was set to: "
    << vm["parameterfile"].as<string>() << ".\n";
    }

    }
    catch(exception& e) {
    cerr << "Error: " << e.what() << "\n";
    }
    catch(...) {
    cerr << "Exception of unknown type!\n";
    }
    }

    Parameter::parameter() : mOutputFolder("./OutputFolder"),
    mDataFolder("Data"), mParameterfile("ParameterFiles/parameters.xml")
    {
    }


    Parameter::~Parameter()
    {
    }
    // --------------------------------


    // Makefile
    NEW_GCC = /afs/ies.auc.dk/sw/released
    3G = /afs/ies.auc.dk/project/3.9G/Private/dist
    CXX = g++
    CPPFLAGS = -g -Wall -pipe -I$(3G)/include -I$(NEW_GCC)/include
    -I$(3G)/../libraries_cpp -pg
    LDFLAGS = -L$(NEW_GCC)/lib -L$(3G)/lib -lboost_filesystem-gcc-d-1_33_1
    -lboost_program_options-gcc-1_33_1 -lxerces-c

    ..SUFFIXES: .c .cpp

    TARGET = TEST

    OBJ = Parameter.o Main.o

    all: $(OBJ) $(TARGET)

    ..cpp.o:
    $(CXX) $(CPPFLAGS) $< -c

    $(TARGET): $(OBJ)
    $(CXX) $(CPPFLAGS) $(OBJ) $(LDFLAGS) -o $@

    purify: $(OBJ)
    purify $(CXX) $(OBJ) -o $(TARGET)

    clean:
    -find . -name "*.o" -exec rm -rf {} \;

    // ----------------


    Now, if I try to run the program without specifing any input argument,
    that is simply as ./TEST, then the program works (note that TEST is the
    name I gave to the executable). If, instead, I try to specify as option
    the path to the parameter file, like:
    ../TEST -p parameters.txt
    (assuming parameters.txt to be on the same level of TEST)
    then I receive the following error:
    Segmentation fault

    If I run the program using GDB then I get the following output:

    Parameter::Initialize (ac=3, av=0xbffff934) at Parameter.h:17
    17 mpParameter->parseCommandLine(ac, av);
    (gdb)

    Program received signal SIGSEGV, Segmentation fault.
    0xb7ad9f97 in std::string::_Rep::_M_grab () from
    /usr/lib/libstdc++.so.5
    (gdb)
    Single stepping until exit from function _ZNSs4_Rep7_M_grabERKSaIcES2_,
    which has no line number information.
    Couldn't get registers: No such process.
    (gdb)
    Single stepping until exit from function _ZNSs4_Rep7_M_grabERKSaIcES2_,
    which has no line number information.
    Cannot fetch general-purpose registers for thread -1215358144: generic
    error
    (gdb)
    Single stepping until exit from function _ZNSs4_Rep7_M_grabERKSaIcES2_,
    which has no line number information.
    Cannot fetch general-purpose registers for thread -1215358144: generic
    error

    Note that this error occurs only when I use Linux. With Visual Studio
    C++ 2005 (under Windows) I don't have problems even if I specify the
    parameter through command line.

    Does anyone have any suggestion on how to solve this problem in Linux?

    Thanks & regards
    Cesco
    cesco, Mar 3, 2006
    #1
    1. Advertising

  2. cesco

    Guest

    cesco wrote:

    > If I run the program using GDB then I get the following output:
    >
    > Parameter::Initialize (ac=3, av=0xbffff934) at Parameter.h:17
    > 17 mpParameter->parseCommandLine(ac, av);
    > (gdb)
    >
    > Program received signal SIGSEGV, Segmentation fault.
    > 0xb7ad9f97 in std::string::_Rep::_M_grab () from
    > /usr/lib/libstdc++.so.5
    > (gdb)
    > Single stepping until exit from function _ZNSs4_Rep7_M_grabERKSaIcES2_,
    > which has no line number information.
    > Couldn't get registers: No such process.


    Instead of trying to continue running the program by stepping, which as
    you can see is not possible after a segfault, try running the 'bt'
    command and look to see where your code calls stop. Much of the time
    the bug you are looking for is in the call stack somewhere and
    99.99999% of the time it is your code.
    , Mar 3, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hardy
    Replies:
    1
    Views:
    3,986
    Rapscallion
    May 16, 2005
  2. Starfox
    Replies:
    2
    Views:
    512
    Starfox
    Jul 25, 2005
  3. Krivenok Dmitry

    Boost program_options trouble

    Krivenok Dmitry, Sep 19, 2005, in forum: C++
    Replies:
    0
    Views:
    353
    Krivenok Dmitry
    Sep 19, 2005
  4. Krivenok Dmitry

    Boost program_options trouble

    Krivenok Dmitry, Sep 19, 2005, in forum: C++
    Replies:
    1
    Views:
    376
    mlimber
    Sep 20, 2005
  5. MorningStar
    Replies:
    2
    Views:
    621
    MorningStar
    Mar 23, 2007
Loading...

Share This Page