[libstdc++ PATCH] Fix leak in _Rb_tree::_M_insert (stl_tree.h)

Chris Jefferson caj@cs.york.ac.uk
Fri Feb 25 14:17:00 GMT 2005


Earl Chew wrote:
> Running the exception test suite from STLPort uncovers a
> leak in _Rb_tree::_M_insert when an exception is thrown from
> a comparator.
> 
> Earl
> 
> --- stl_tree.h.orig    2004-12-12 08:14:30.000000000 -0800
> +++ stl_tree.h    2005-02-24 16:57:15.000000000 -0800
> @@ -781,9 +781,17 @@
>        _Link_type __z = _M_create_node(__v);
>        bool __insert_left;
> 
> -      __insert_left = __x != 0 || __p == _M_end()
> -                  || _M_impl._M_key_compare(_KeyOfValue()(__v),
> -                        _S_key(__p));
> +      try
> +    {
> +          __insert_left = __x != 0 || __p == _M_end()
> +                          || _M_impl._M_key_compare(_KeyOfValue()(__v),
> +                                                    _S_key(__p));
> +        }
> +      catch(...)
> +    {
> +      destroy_node(__z);
> +      __throw_exception_again;
> +    }
> 
>        _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
>                      this->_M_impl._M_header);
> 

I apologise if I'm saying something really stupid, but from my looking 
at the source code to stl_tree.h, it looks like _M_create_node just 
allocates memory and doesn't actually do any tree based things. 
Therefore why not just delay declaring the _Link_type until after the 
__insert_left initalisation? Then there is no need for a try{...}catch{...}

Chris



More information about the Libstdc++ mailing list