开发者

c++ exception from constructor core dumps on freebsd but not on linux or mac os x

Why does the program listed below run on mac osx and linux but not on freebsd?

The freebsd core dumps after the exception is thrown with the following message:

terminate called after throwing an instance of 'ObjectException'
  what():  error not allowed
[1]    28946 abort (core dumped)  .开发者_JAVA技巧/bin/main

On all three platforms I use gnu compilers

freebsd g++ --version : g++ (GCC) 4.2.1 20070719

mac os x g++ --version: i686-apple-darwin10-g++-4.2.1

linux g++ --version: g++ (Gentoo 4.3.3 p1.0, pie-10.1.5)

freebsd uname -a: 8.1-RELEASE FreeBSD 8.1-RELEASE

I use cmake to create the Makefile, so these are also similar on each platform

Here is the listing:

Header

#ifndef GUARD_Object_h
#define GUARD_Object_h

#include "boost/scoped_ptr.hpp"
#include "string"
#include "exception"

using std::string;

class Object
{
private:
    boost::scoped_ptr<string> _name;
public:
    Object(const string&);
    string getName();
};

class ObjectException:public std::exception
{
    virtual const char* what() const throw()
    {
        return "error not allowed";
    }
};

#endif

Main

#include "Object.h"

Object::Object(const string &name):_name (new string)
{
    *_name = name;
   if(*_name == "error") 
   {  
        throw ObjectException();
   }  
}

string Object::getName() 
{
    return *_name;
}

Main

#include <iostream>
#include "Object.h"

int main() 
{
    try
    {
        new Object("error");
    } catch(ObjectException& ) {
        std::cout << "error found" << std::endl;
    }
}


Works for me on FreeBSD 8.1-RELEASE-p2 and OSX 10.6.6.

You could have a bad version of libstdc++ in your search path. When I link on FreeBSD 8.1, I get the following output from ldd:

janm@midgard: test3 $ ldd a.out
a.out:
    libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x800649000)
    libm.so.5 => /lib/libm.so.5 (0x800854000)
    libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x800973000)
    libc.so.7 => /lib/libc.so.7 (0x800a80000)

If yours looks significantly different you might have a runtime library mismatch. If you have a problem, you can try linking with "g++ -static" to statically link the runtime library at link time. If that works, you need to fix your machine.

Update:

It looks like your compiler and runtime library don't match, and that is probably caused by cmake configuration problem. You probably have installed a version of gcc into /usr/local, probably using a port.

If you compile with system gcc, you need to link with the runtime libraries in /usr/lib. If you compile with a gcc in /usr/local, you need to link with the libraries in /usr/local. If you're planning on shipping the binaries around, you should probably use system gcc or statically link.

Another option is to just uninstall the gcc port and try again, assuming you don't really care about the gcc port.


I compiled and ran your code on FreeBSD 8.1 without any problem (using g++ -I/usr/local/include -g Main.cpp Object.cpp). Maybe you need to upgrade (reinstall) boost on that computer?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜