jeudi 1 février 2018

Why boost::diagnostic_information crashed and how to fix it?

I try to use boost::exception but have the trouble. I write following code:

struct BaseExceptionXXX : public virtual std::exception, public virtual boost::exception
{
public:
    BaseExceptionXXX()
    { };
    virtual ~BaseExceptionXXX() {};

    BaseExceptionXXX(char const* const message)
        : std::exception(message)
    { }

    BaseExceptionXXX(const std::exception& e)
        : std::exception(e)
    { }

    BaseExceptionXXX(const BaseException& e)
        : std::exception(e)
        , boost::exception(e)
    { }

    bool IsEmpty() const
    {
        const std::string what_err = std::exception::what();
        return (what_err.empty() && boost::get_error_info<UserErrorInfo>(*this) == nullptr);
    }

    const char* what() const throw() override
    {
        return boost::diagnostic_information(*this).c_str(); //<-- crash here
    }
};


int main()
{
    std::string exception_description;

    try
    {
        BOOST_THROW_EXCEPTION(BaseExceptionXXX("hello exception"));
    }
    catch (BaseExceptionXXX& ex)
    {
        exception_description = ex.what(); //<-- crash here
    }
}

But it crashed in the function: boost::diagnostic_information(*this). It crashed by the reason: Stack overflow

Why it happens and how to use the boost::exception in the correct way?

Boost version - 1.66

MSVS2017 version - 15.5.5

Aucun commentaire:

Enregistrer un commentaire