473,511 Members | 15,364 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

segmentation fault using boost::program_options on Linux

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::options_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

Mar 3 '06 #1
1 5345

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 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2659
by: Geiregat Jonas | last post by:
I'm running a linux system I would like to create a login system based upon the users and passwd of my linux box how could I do this ?
1
1749
by: Steven Burn | last post by:
Thats the headline this morning. In the latest issue of The Register (my favourite online newsletter), there's a story (see URL below) about Microsoft using Linux temporarily, for their...
1
7773
by: Hardy | last post by:
Hi, just come into the boost world. just the first.cpp in the program_options examples, with many link error... devc++4.9.9.2, gcc 3.4.2, can I get your opinions on this problem? thank you~ ...
0
402
by: Krivenok Dmitry | last post by:
I use boost program_options library in my project. But I have one trouble and can't solve it :( I writing simple wrapper class and use generic selector:...
3
5112
by: mairhtin o'feannag | last post by:
Hello, Since DB2 Cluster Certification Guide is out of print and I cannot seem to get my hands on a copy, is there a publication that would give me a "cookbook" approach to setting up a cluster...
1
3368
by: zade | last post by:
Hi all boost::program_options needs be builled into libraries, static or dynamic. I use MSVC7.1 to build the boost::program_options for my project .In my .cpp file , I write: namespace po =...
3
1801
by: danbrownlow | last post by:
Hello there, I'm new and need some guidance. Firstly, I started learning c++ and used an IDE called Dev C++ on windows, shortly after I switched over to Linux and I'm using a IDE called Anjuta. ...
3
4495
by: deardp | last post by:
hi : i need to convert an image (ex: jpeg or gif) into binary form (which can uploadavle to a server) using unix or linux script. pls pass me if u have any code. regards Durga
3
1750
by: Man4ish | last post by:
Hi, Can anyone tell me how linux (ubuntu) can be used on windows machine(Laptop). Since i am getting net in windows not in linux(dual boot).How can we do itI need to install the softwares in the...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.