segmentation fault using boost::program_options on Linux

C

cesco

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
 
R

roberts.noah

cesco said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top