diff --git a/src/Connection.cpp b/src/Connection.cpp index 37496e6..e8c038c 100644 --- a/src/Connection.cpp +++ b/src/Connection.cpp @@ -22,6 +22,7 @@ Connection::Connection(const AMQP::Address& address, int timeout): timeout(timeo Connection::~Connection() { delete pimpl; + pimpl = nullptr; } void Connection::connect() { diff --git a/src/linux/ConnectionImpl.cpp b/src/linux/ConnectionImpl.cpp index b07071d..4248b6e 100644 --- a/src/linux/ConnectionImpl.cpp +++ b/src/linux/ConnectionImpl.cpp @@ -13,8 +13,8 @@ ConnectionImpl::ConnectionImpl(const AMQP::Address& address) : sslInited = true; } eventLoop = event_base_new(); - handler = new TCPHandler(eventLoop); - connection = new AMQP::TcpConnection(handler, address); + handler.reset(new TCPHandler(eventLoop)); + connection.reset(new AMQP::TcpConnection(handler.get(), address)); thread = std::thread(ConnectionImpl::loopThread, this); } @@ -25,8 +25,8 @@ ConnectionImpl::~ConnectionImpl() { } event_base_loopbreak(eventLoop); thread.join(); - delete connection; - delete handler; + connection.reset(nullptr); + handler.reset(nullptr); event_base_free(eventLoop); } @@ -34,7 +34,11 @@ ConnectionImpl::~ConnectionImpl() { void ConnectionImpl::loopThread(ConnectionImpl* thiz) { event_base* loop = thiz->eventLoop; while(!thiz->connection->closed()) { - event_base_loop(loop, EVLOOP_NONBLOCK); + try{ + event_base_loop(loop, EVLOOP_NONBLOCK); + }catch(std::exception& ex){ + Biterp::Logging::error("Channel loop error: " + std::string(ex.what())); + } } } @@ -50,7 +54,7 @@ void ConnectionImpl::openChannel(std::unique_ptr& channel) { std::condition_variable cv; bool ready = false; - channel.reset(new AMQP::TcpChannel(connection)); + channel.reset(new AMQP::TcpChannel(connection.get())); channel->onReady([&]() { std::unique_lock lock(m); ready = true; diff --git a/src/linux/ConnectionImpl.h b/src/linux/ConnectionImpl.h index 10c3f15..3ee5536 100644 --- a/src/linux/ConnectionImpl.h +++ b/src/linux/ConnectionImpl.h @@ -21,8 +21,8 @@ class ConnectionImpl{ private: event_base* eventLoop; - TCPHandler* handler; - AMQP::TcpConnection* connection; + std::unique_ptr handler; + std::unique_ptr connection; std::unique_ptr trChannel; std::thread thread; diff --git a/src/windows/ConnectionImpl.cpp b/src/windows/ConnectionImpl.cpp index 4e5bc64..91f77fc 100644 --- a/src/windows/ConnectionImpl.cpp +++ b/src/windows/ConnectionImpl.cpp @@ -4,8 +4,8 @@ ConnectionImpl::ConnectionImpl(const AMQP::Address& address) : handler(address.hostname(), address.port(), address.secure()), trChannel(nullptr) { - connection = new AMQP::Connection(&handler, address.login(), address.vhost()); - handler.setConnection(connection); + connection.reset(new AMQP::Connection(&handler, address.login(), address.vhost())); + handler.setConnection(connection.get()); thread = std::thread(SimplePocoHandler::loopThread, &handler); } @@ -16,7 +16,7 @@ ConnectionImpl::~ConnectionImpl() { connection->close(); } thread.join(); - delete connection; + connection.reset(nullptr); } void ConnectionImpl::openChannel(std::unique_ptr& channel) { @@ -29,7 +29,7 @@ void ConnectionImpl::openChannel(std::unique_ptr& channel) { std::mutex m; std::condition_variable cv; bool ready = false; - channel.reset(new AMQP::Channel(connection)); + channel.reset(new AMQP::Channel(connection.get())); channel->onReady([&]() { std::unique_lock lock(m); ready = true; diff --git a/src/windows/ConnectionImpl.h b/src/windows/ConnectionImpl.h index 6364c3c..0eb22bd 100644 --- a/src/windows/ConnectionImpl.h +++ b/src/windows/ConnectionImpl.h @@ -18,7 +18,7 @@ class ConnectionImpl{ private: SimplePocoHandler handler; - AMQP::Connection* connection; + std::unique_ptr connection; std::unique_ptr trChannel; std::unique_ptr rcChannel; std::thread thread;