Boost logo

Boost Users :

From: Merrill Cornish (merrill.cornish_at_[hidden])
Date: 2005-11-21 11:41:15


I posted a question earlier about why I couldn't get Boost 1.33.0 program_options to "see"
my positional parameter and why it bombed on unexpected options.

Volodya pointed out that an illegal option throws an exception and asked if I had a catch.
I felt like a fool for forgetting exceptions and through that was the problem. Then I
looked at the code again.

The method that uses program_options to read the command line had no try/catch, but the
main program that called that method DID have the call wrapped in a try that has catches
both for std::exception and (...). And yet, it wasn't trapping anything.

Between ZoneAlert and Windows, I *think* I know the problem: something is attempting to
access NULL, so I'm getting an OS memory error that C++ exceptions can't catch.

I moved try's and catch's around in the code until I found the offending statement:

   po::variables_map vm;

...a declaration of all things. Here's an excerpt of the code

        po::options_description desc("Options");
        desc.add_options()
                ("help,h", " Display this help message.")
                ("version", " Display the version.")
                // ... other options
        ;//add-options()

        // specify the single, required graph file positional parameter
        po::positional_options_description pd;
        pd.add("graph_file", 1);
        po::variables_map vm; //<==============
try {

        po::store(po::command_line_parser(argc, argv).
              options(desc).
              positional(pd).
              run(), vm);
        po::notify(vm);

        // ... other code that references vm
}//try
catch(std::exception e) {
    cerr << "Exception:" << endl;
    cerr << e.what() << endl;
    exit(EXIT_FAILURE);
}//catch
catch(...) {
    cerr << "An unknown exception occurs while parsing :
                "command line arguments." << endl;
    exit(EXIT_FAILURE);
}//catch

Using the 'try' in the position shown above, if I input an unexpected command
line options, an exceptioin is thrown whose what() method returns "St9exception".
That's it. Just "St9exception". I can't find that string in the Boost source anywhere.

However, if I move the "try" up one statement so it's above
"po::variables_map vm;", then there's no exceptoin, but the access to location
0.

That's one picky declaration.

Merrill


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net