Re: [Boost-bugs] [Boost C++ Libraries] #6844: [function] Memory leaks if used with allocator and throwing copy-c'tor of functor

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #6844: [function] Memory leaks if used with allocator and throwing copy-c'tor of functor
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2012-04-29 11:29:25


#6844: [function] Memory leaks if used with allocator and throwing copy-c'tor of
functor
--------------------------------------------------------------+-------------
  Reporter: Daniel Krügler <daniel.kruegler@…> | Owner: dgregor
      Type: Bugs | Status: new
 Milestone: To Be Determined | Component: function
   Version: Boost 1.49.0 | Severity: Problem
Resolution: | Keywords:
--------------------------------------------------------------+-------------

Comment (by Daniel Krügler <daniel.kruegler@…>):

 I should mention that the original test contained a hack to simplify the
 allocator's emulation. To demonstrate that the test is not based on false
 usage of the allocators API here follows a corrected version, which does
 not behave differently in regard to the relevant point:

 {{{
 #include "boost/function.hpp"
 #include <memory>
 #include <iostream>

 struct F {
   static bool dothrow;
   unsigned char
 prevent_short_object_opt[sizeof(boost::detail::function::function_buffer)
 + 1];
   F(){}
   F(const F&) {
     if (dothrow)
       throw 0;
   }
   void operator()() {}
 };

 bool F::dothrow = false;
 int alloc_cnt = 0;

 template<class T>
 struct my_alloc : std::allocator<T>
 {
   my_alloc() : std::allocator<T>() {}
   template<class U>
   my_alloc(const my_alloc<U>& other) : std::allocator<T>(other) {}
   template<class Other>
   struct rebind
   {
     typedef my_alloc<Other> other;
   };
   void construct(typename std::allocator<T>::pointer p, const T& val)
   {
     F::dothrow = true;
     ::new((void *)p) T(val);
   }
   void deallocate(typename std::allocator<T>::pointer p,
                       typename std::allocator<T>::size_type n)
   {
     --alloc_cnt;
     std::cout << "deallocate: " << alloc_cnt << std::endl;
     return std::allocator<T>::deallocate(p, n);
   }
   typename std::allocator<T>::pointer allocate(typename
 std::allocator<T>::size_type n)
   {
     ++alloc_cnt;
     std::cout << "allocate: " << alloc_cnt << std::endl;
     return std::allocator<T>::allocate(n);
   }
 };

 int main() {
   F f;
   my_alloc<F> a;
   try {
     boost::function<void()> fu(f, a);
   } catch (int) {
     std::cout << "Caught expected - allocation count: " << alloc_cnt <<
 std::endl;
   }
 }
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/6844#comment:1>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:09 UTC