From bda5675f830a35df11e2d6265fcfb276b0b63a6f Mon Sep 17 00:00:00 2001 From: Etienne Cimon Date: Thu, 5 Nov 2015 21:32:26 -0500 Subject: [PATCH] Free TCP Context on connection failure Implements RAII-style freeing mechanism for manually allocated object --- source/vibe/core/drivers/libevent2.d | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/vibe/core/drivers/libevent2.d b/source/vibe/core/drivers/libevent2.d index 0d33c47d9b..800960b090 100644 --- a/source/vibe/core/drivers/libevent2.d +++ b/source/vibe/core/drivers/libevent2.d @@ -290,6 +290,7 @@ final class Libevent2Driver : EventDriver { if( !buf_event ) throw new Exception("Failed to create buffer event for socket."); auto cctx = TCPContextAlloc.alloc(m_core, m_eventLoop, sockfd, buf_event, bind_addr, addr); + scope(failure) TCPContextAlloc.free(cctx); bufferevent_setcb(buf_event, &onSocketRead, &onSocketWrite, &onSocketEvent, cctx); if( bufferevent_enable(buf_event, EV_READ|EV_WRITE) ) throw new Exception("Error enabling buffered I/O event for socket."); @@ -364,6 +365,7 @@ final class Libevent2Driver : EventDriver { auto core = getThreadLibeventDriverCore(); // Add an event to wait for connections auto ctx = TCPContextAlloc.alloc(core, evloop, handler_context.listenfd, null, handler_context.bind_addr, NetworkAddress()); + scope(failure) TCPContextAlloc.free(ctx); ctx.connectionCallback = handler_context.connection_callback; ctx.listenEvent = event_new(evloop, handler_context.listenfd, EV_READ | EV_PERSIST, &onConnect, ctx); ctx.listenOptions = handler_context.options; @@ -889,7 +891,7 @@ final class Libevent2UDPConnection : UDPConnection { // create a context for storing connection information m_ctx = TCPContextAlloc.alloc(driver.m_core, driver.m_eventLoop, sockfd, null, bind_addr, NetworkAddress()); - + scope(failure) TCPContextAlloc.free(m_ctx); m_ctx.listenEvent = event_new(driver.m_eventLoop, sockfd, EV_READ|EV_PERSIST, &onUDPRead, m_ctx); if (!m_ctx.listenEvent) throw new Exception("Failed to create buffer event for socket."); }