Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception safety with allocators in any #16

Closed
EricWF opened this issue Apr 27, 2014 · 3 comments
Closed

Exception safety with allocators in any #16

EricWF opened this issue Apr 27, 2014 · 3 comments
Assignees
Labels
Milestone

Comments

@EricWF
Copy link

EricWF commented Apr 27, 2014

static void clone (data_type const& source, data_type& data) {
    allocator_type alloc { };
    auto const& value = *static_cast<Type* const>(source);
    auto pointer = allocator_traits::allocate(alloc, 1);
    allocator_traits::construct(alloc, pointer, value);
    data = pointer;
  }

If the construction throws, the allocated memory is leaked. The best way (I've seen) to work around this issues is to store the allocated memory in a unique_ptr with a custom dtor that simply calls allocator::deallocate.

For reference here is how I solved this problem in my any implementation (Which is based off of libc++'s std::function).

storage_base* copy() const
{
    using NewAlloc = typename Alloc::template rebind<storage_type>::other;
    NewAlloc a(m_pair.second());
    using Dtor = allocator_destructor<NewAlloc>;
    std::unique_ptr<storage_type, Dtor> tmp(a.allocate(1), Dtor(a, 1));
    ::new ((void*)tmp.get()) storage_type(
        m_pair.first(), Alloc(a)
        );
    return tmp.release();
}
@bruxisma bruxisma added this to the 1.1 Release milestone Apr 28, 2014
@bruxisma bruxisma added the bug label Apr 28, 2014
@bruxisma bruxisma self-assigned this Apr 28, 2014
@tarqd
Copy link

tarqd commented May 21, 2014

Could also use a scope guard, no need to hit the heap for that (plus there's one in this library!). Basically the equivalent of:

static void clone (data_type const& source, data_type& data) {
    struct scope_guard { 
        allocator_type& m_alloc; 
        typename allocator_traits::pointer m_ptr;
        bool dismissed = false; 
         void dismiss() { dismissed = true; }
        ~scope_guard() { if (!dismissed) allocator_traits::deallocate(m_alloc, m_ptr); }
    };
    allocator_type alloc { };
    auto const& value = *static_cast<Type* const>(source);
    auto pointer = allocator_traits::allocate(alloc, 1);
    scope_guard guard { alloc, pointer };
    allocator_traits::construct(alloc, pointer, value);
    guard.dismiss(); // if an exception throws, the memory will be deallocated in the guard dtor
    data = pointer;
  }

@EricWF
Copy link
Author

EricWF commented May 21, 2014

Seems like a very reasonable way to do it without having to add a new class. What do you by "no need to hit the heap"?

@tarqd
Copy link

tarqd commented May 21, 2014

Whoops for some reason I thought unique_ptr would be making a heap allocation but obviously it doesn't. It's been a long day haha. It does fit the use-case for scope_guard very nicely though (basically unique_ptr is acting like one).

Essentially both of our examples are doing the same thing, but with the scope_guard in this library (after it adds dismissal support) it'd be

static void clone (data_type const& source, data_type& data) {
    allocator_type alloc { };
    auto const& value = *static_cast<Type* const>(source);
    auto pointer = allocator_traits::allocate(alloc, 1);
    auto guard = make_scope_guard([&] { allocator_traits::deallocate(alloc, pointer); });
    allocator_traits::construct(alloc, pointer, value);
    guard.dismiss(); // if an exception throws, the memory will be deallocated in the guard dtor
    data = pointer;
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants