Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::exception - leaking error_info
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2009-02-20 22:17:55


On Fri, Feb 20, 2009 at 3:00 PM, Bruce Laing
<bruce.laing_at_[hidden]> wrote:
> This program seems to be leaking boost::exception::error_info objects.
> Is there something missing here?
>
> - Bruce
>
>
> typedef boost::error_info< struct tag_my_error, std::string >
> some_error_info;
>
> struct an_exception : public boost::exception, public std::exception {};
>
> void throw_func()
> {
> BOOST_THROW_EXCEPTION
> (
> an_exception()
> // no leak if next line is commented out.
> << some_error_info( std::string( "error message" ) )
> );
> }
> .
> .
> .
> BOOST_AUTO_TEST_CASE( test_exceptions )
> {
> try
> {
> throw_func()
> }
> catch( an_exception& )
> {
> }
> }

What platform? Do you have a complete program that shows the problem?

This test seems to work as expected:

#include "boost/exception.hpp"

class
counter
        {
        int & c_;
        counter & operator=( counter const & );

        public:

        explicit
        counter( int & c ):
                c_(c)
                {
                ++c_;
                }

        counter( counter const & x ):
                c_(x.c_)
                {
                ++c_;
                }

        ~counter()
                {
                --c_;
                }
        };

typedef boost::error_info< struct tag_my_error, counter > some_error_info;

struct an_exception : public boost::exception, public std::exception {};

int c;

void
throw_func()
        {
        BOOST_THROW_EXCEPTION
                (
                an_exception() << some_error_info( counter(c) )
                );
        }

int
main()
        {
        c=0;
        try
                {
                throw_func();
                }
        catch( an_exception & )
                {
                assert(c>0);
                }
        assert(!c);
        }

Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode


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