Fixing memory leak in TurnServer.

If the test TURN server received two allocate requests from the same
address, it was replacing the old allocation but not deleting it.

Also switching to std::unique_ptr to make it less likely for this to
pop up again.

Review-Url: https://codereview.webrtc.org/2114063002
Cr-Commit-Position: refs/heads/master@{#13449}
This commit is contained in:
deadbeef 2016-07-12 11:04:50 -07:00 committed by Commit bot
parent e5a246f693
commit 9794366ff0
3 changed files with 8 additions and 10 deletions

View File

@ -99,7 +99,7 @@ class TestTurnServer : public TurnAuthInterface {
for (TurnServer::AllocationMap::const_iterator it = map.begin();
it != map.end(); ++it) {
if (src == it->first.src()) {
return it->second;
return it->second.get();
}
}
return NULL;

View File

@ -124,11 +124,6 @@ TurnServer::TurnServer(rtc::Thread* thread)
}
TurnServer::~TurnServer() {
for (AllocationMap::iterator it = allocations_.begin();
it != allocations_.end(); ++it) {
delete it->second;
}
for (InternalSocketMap::iterator it = server_sockets_.begin();
it != server_sockets_.end(); ++it) {
rtc::AsyncPacketSocket* socket = it->first;
@ -429,7 +424,7 @@ bool TurnServer::ValidateNonce(const std::string& nonce) const {
TurnServerAllocation* TurnServer::FindAllocation(TurnServerConnection* conn) {
AllocationMap::const_iterator it = allocations_.find(*conn);
return (it != allocations_.end()) ? it->second : NULL;
return (it != allocations_.end()) ? it->second.get() : nullptr;
}
TurnServerAllocation* TurnServer::CreateAllocation(TurnServerConnection* conn,
@ -445,7 +440,7 @@ TurnServerAllocation* TurnServer::CreateAllocation(TurnServerConnection* conn,
TurnServerAllocation* allocation = new TurnServerAllocation(this,
thread_, *conn, external_socket, key);
allocation->SignalDestroyed.connect(this, &TurnServer::OnAllocationDestroyed);
allocations_[*conn] = allocation;
allocations_[*conn].reset(allocation);
return allocation;
}
@ -518,8 +513,10 @@ void TurnServer::OnAllocationDestroyed(TurnServerAllocation* allocation) {
}
AllocationMap::iterator it = allocations_.find(*(allocation->conn()));
if (it != allocations_.end())
if (it != allocations_.end()) {
it->second.release();
allocations_.erase(it);
}
}
void TurnServer::DestroyInternalSocket(rtc::AsyncPacketSocket* socket) {

View File

@ -161,7 +161,8 @@ class TurnRedirectInterface {
// Not yet wired up: TCP support.
class TurnServer : public sigslot::has_slots<> {
public:
typedef std::map<TurnServerConnection, TurnServerAllocation*> AllocationMap;
typedef std::map<TurnServerConnection, std::unique_ptr<TurnServerAllocation>>
AllocationMap;
explicit TurnServer(rtc::Thread* thread);
~TurnServer();