From 21b4f8c501d67ed8ecf6dedbdd55064a5c8f823c Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 3 Dec 2014 12:04:56 -0800 Subject: [PATCH] libuv handles must be deallocated asynchronously Fixes #659 --- include/mbgl/util/uv_detail.hpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/include/mbgl/util/uv_detail.hpp b/include/mbgl/util/uv_detail.hpp index 099e08d4f..b6b9be208 100644 --- a/include/mbgl/util/uv_detail.hpp +++ b/include/mbgl/util/uv_detail.hpp @@ -14,8 +14,10 @@ namespace uv { template -void close(T* handle) { - uv_close(reinterpret_cast(handle), nullptr); +void close(std::unique_ptr ptr) { + uv_close(reinterpret_cast(ptr.release()), [](uv_handle_t* handle) { + delete reinterpret_cast(handle); + }); } class thread : public mbgl::util::noncopyable { @@ -75,19 +77,21 @@ private: class async : public mbgl::util::noncopyable { public: inline async(uv_loop_t* loop, std::function fn_) - : fn(fn_) { - a.data = this; - if (uv_async_init(loop, &a, async_cb) != 0) { + : a(new uv_async_t) + , fn(fn_) + { + a->data = this; + if (uv_async_init(loop, a.get(), async_cb) != 0) { throw std::runtime_error("failed to initialize async"); } } inline ~async() { - close(&a); + close(std::move(a)); } inline void send() { - if (uv_async_send(&a) != 0) { + if (uv_async_send(a.get()) != 0) { throw std::runtime_error("failed to async send"); } } @@ -101,7 +105,7 @@ private: reinterpret_cast(a->data)->fn(); } - uv_async_t a; + std::unique_ptr a; std::function fn; };