Boost logo

Boost Users :

Subject: Re: [Boost-users] Program options (1.43.0) problem on Mac OS 10.6: pointer being freed not allocated
From: Christopher Jefferson (chris_at_[hidden])
Date: 2010-06-02 03:28:44


I'm assuming your code works correctly on the apple-supplied compiler?

The problem is a bug in Mac OS X Snow Leopard (10.6), where the system binaries are compiled with _GLIBCXX_FULLY_DYNAMIC_STRING, but the headers do not define that term. This is not a problem with your code or program options. A smaller example which produces the same problem:

#include <boost/program_options.hpp>
#include <iostream>

namespace po = boost::program_options;

int main(int argc, char** argv)
{
po::options_description desc("Options");
desc.add_options()("help", "Produce help message");
std::cout << desc;
}

But it is easy to produce a tiny library which just constructs and passes and empty std::string, which shows the same problem.

I have tried various means of working around this problem, including editing the system headers. The method I have come up with that seems most reliable is below, in two steps.

1) Dealing with dynamic string.

Did you build the compiler yourself, or get it from macports?

If you built it yourself, configure with ' --enable-fully-dynamic-string '. The macports compiler should have this option. If you want to check, look wherever the headers for your compiler are stored with g++ -print-search-dirs, and look in those directories for a file called bits/c++config.h , and see if it contains the line ' #define _GLIBCXX_FULLY_DYNAMIC_STRING '

2) Build boost with the same compiler as you want to run it with.

Add a line like:

using darwin : : g++-mp-4.4 ;

To your project-config.jam (in the directory you build boost from), or user-config.jam. This ensures you build boost using the same compiler you will use.

Note that after doing this you will probably still find it impossible to get good quality debugging, I have been unable to get a gdb to install which accepts the debugging output from g++ 4.4 or 4.5. My advice is to install a Linux VM, to do development and debugging.

On 2 Jun 2010, at 05:20, Robert McLay wrote:

> I'm using boost 1.43.0. I have enclose a program which uses
> boost::program_options on a Mac OS 10.6 and when I run it, I get
> errors. I have compiled boost with g++ version 4.4.1 and version 4.5.0
> and I get the same errors. I have tried the version of boost (1.42.0)
> that comes from macports as well as downloaded version that I have build.
>
> clo(87082) malloc: *** error for object 0xa0136db0: pointer being freed
> was not allocated.
>
> The program is given below. I have run the same program with boost
> 1.43.0 under linux and I don't get any errors. It is also completely
> clean of any errors when run through valgrind. The program is an
> example showing problem I'm having. It has been extracted from a larger
> program. Am I correctly using the library
> or is there a problem with boost (and only on a Mac?)
>
> Thanks for any help.
> -----------------------------------------------------------------------------------------------------------------------
>
>
> #include<iostream>
> #include<string>
> #include<boost/program_options.hpp>
> class CmdLineOptions
> {
> public:
> CmdLineOptions(int argc, char* argv[]);
>
> bool is_good() {return m_isGood;}
>
> public:
> bool negative;
> int clustSize;
> float rebuildRatio;
> std::string input_file;
>
> private:
> bool m_isGood;
> };
>
> namespace po = boost::program_options;
> CmdLineOptions::CmdLineOptions(int argc, char* argv[])
> : m_isGood(true)
> {
> po::options_description visible("Options");
> visible.add_options()
> ("version,v", "print version string")
> ("help,h", "produce help message")
> ("question,?", "produce help message")
> ("clust_size,s", po::value<int>()->default_value(30),
> "cluster size")
> ("rebuild_ratio,r", po::value<float>()->default_value(0.5),
> "Rebuild ratio")
> ("negative,n", "Allow negative distances")
> ;
>
>
> // Hidden options, will be allowed both on command line
> po::options_description hidden("Hidden options");
> hidden.add_options()
> ("input-file", po::value<std::string>(), "input file")
> ;
>
> po::options_description cmdline_options;
> cmdline_options.add(visible).add(hidden);
>
> po::positional_options_description p;
> p.add("input-file", -1);
>
> po::variables_map vm;
> store(po::command_line_parser(argc, argv).
> options(cmdline_options).positional(p).run(), vm);
>
> if (vm.count("input-file") == 0 || vm.count("help") ||
> vm.count("question"))
> {
> std::cerr<< "\nUsage:\n"
> << " "<< argv[0]<< " [options] input_file\n\n"
> << visible<< std::endl;
> m_isGood = false;
> return;
> }
>
> clustSize = vm["clust_size"].as<int>();
> input_file = vm["input-file"].as<std::string>();
> rebuildRatio = vm["rebuild_ratio"].as<float>();
> negative = vm.count("negative")> 0;
> }
>
>
>
> int main(int argc, char * argv[])
> {
>
> // Parse Command line options
> CmdLineOptions cmd(argc, argv);
> if (! cmd.is_good())
> return 1;
>
> std::cout<< "clustSize: "<< cmd.clustSize<< " rebuild factor: "<<
> cmd.rebuildRatio<< "\n";
>
> }
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


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