Authserver: Fix a memory leak

This commit is contained in:
Carbenium
2016-01-23 01:05:43 +01:00
parent 0d80fee45a
commit c9a462c693
2 changed files with 24 additions and 5 deletions

View File

@@ -30,7 +30,7 @@ public:
AsyncAcceptor(boost::asio::io_service& ioService, std::string const& bindIp, uint16 port) :
_acceptor(ioService, tcp::endpoint(boost::asio::ip::address::from_string(bindIp), port)),
_socket(ioService)
_socket(ioService), _closed(false)
{
}
@@ -54,14 +54,24 @@ public:
TC_LOG_INFO("network", "Failed to initialize client's socket %s", err.what());
}
}
AsyncAcceptManaged(mgrHandler);
if (!_closed)
AsyncAcceptManaged(mgrHandler);
});
}
void Close()
{
if (_closed.exchange(true))
return;
boost::system::error_code err;
_acceptor.close(err);
}
private:
tcp::acceptor _acceptor;
tcp::socket _socket;
std::atomic<bool> _closed;
};
template<class T>
@@ -83,7 +93,8 @@ void AsyncAcceptor::AsyncAccept()
}
// lets slap some more this-> on this so we can fix this bug with gcc 4.7.2 throwing internals in yo face
this->AsyncAccept<T>();
if (!_closed)
this->AsyncAccept<T>();
});
}

View File

@@ -33,7 +33,7 @@ class SocketMgr
public:
virtual ~SocketMgr()
{
delete[] _threads;
ASSERT(!_threads && !_acceptor && !_threadCount, "StopNetwork must be called prior to SocketMgr destruction");
}
virtual bool StartNetwork(boost::asio::io_service& service, std::string const& bindIp, uint16 port)
@@ -68,10 +68,18 @@ public:
virtual void StopNetwork()
{
_acceptor->Close();
if (_threadCount != 0)
for (int32 i = 0; i < _threadCount; ++i)
_threads[i].Stop();
delete _acceptor;
_acceptor = nullptr;
delete[] _threads;
_threads = nullptr;
_threadCount = 0;
Wait();
}