| 93 | // Helper to complete function invocation. |
| 94 | template <typename Function, typename Alloc> |
| 95 | static void complete(impl_base* base, bool call) |
| 96 | { |
| 97 | // Take ownership of the function object. |
| 98 | impl<Function, Alloc>* i(static_cast<impl<Function, Alloc>*>(base)); |
| 99 | Alloc allocator(i->allocator_); |
| 100 | typename impl<Function, Alloc>::ptr p = { |
| 101 | detail::addressof(allocator), i, i }; |
| 102 | |
| 103 | // Make a copy of the function so that the memory can be deallocated before |
| 104 | // the upcall is made. Even if we're not about to make an upcall, a |
| 105 | // sub-object of the function may be the true owner of the memory |
| 106 | // associated with the function. Consequently, a local copy of the function |
| 107 | // is required to ensure that any owning sub-object remains valid until |
| 108 | // after we have deallocated the memory here. |
| 109 | Function function(ASIO_MOVE_CAST(Function)(i->function_)); |
| 110 | p.reset(); |
| 111 | |
| 112 | // Make the upcall if required. |
| 113 | if (call) |
| 114 | { |
| 115 | asio_handler_invoke_helpers::invoke(function, function); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | impl_base* impl_; |
| 120 | }; |