From 90c4d6624bb2d22cab215e04a2cc4c2ac09253e7 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 20 Apr 2012 16:21:43 +0200 Subject: [PATCH] Fixed weak ptr deref bug. --- base/application.cpp | 19 +++++++++++++------ base/timer.cpp | 11 ++++++++--- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/base/application.cpp b/base/application.cpp index 8a1eeb3d9..20ba6561c 100644 --- a/base/application.cpp +++ b/base/application.cpp @@ -59,11 +59,17 @@ void Application::RunEventLoop(void) FD_ZERO(&writefds); FD_ZERO(&exceptfds); - for (Socket::CollectionType::iterator i = Socket::Sockets.begin(); i != Socket::Sockets.end(); i++) { + Socket::CollectionType::iterator prev, i; + for (i = Socket::Sockets.begin(); i != Socket::Sockets.end(); ) { Socket::Ptr socket = i->lock(); - if (socket == NULL) + prev = i; + i++; + + if (!socket) { + Socket::Sockets.erase(prev); continue; + } int fd = socket->GetFD(); @@ -106,15 +112,16 @@ void Application::RunEventLoop(void) EventArgs ea; ea.Source = shared_from_this(); - Socket::CollectionType::iterator prev, i; for (i = Socket::Sockets.begin(); i != Socket::Sockets.end(); ) { + Socket::Ptr socket = i->lock(); + prev = i; i++; - Socket::Ptr socket = prev->lock(); - - if (socket == NULL) + if (!socket) { + Socket::Sockets.erase(prev); continue; + } int fd = socket->GetFD(); diff --git a/base/timer.cpp b/base/timer.cpp index 1e28bab7b..b7b9f0341 100644 --- a/base/timer.cpp +++ b/base/timer.cpp @@ -42,12 +42,17 @@ void Timer::CallExpiredTimers(void) time(&now); - for (Timer::CollectionType::iterator i = Timers.begin(); i != Timers.end(); ) { - Timer::Ptr timer = Timer::Ptr(*i); + Timer::CollectionType::iterator prev, i; + for (i = Timers.begin(); i != Timers.end(); ) { + Timer::Ptr timer = i->lock(); + + prev = i; i++; - if (timer == NULL) + if (!timer) { + Timers.erase(prev); continue; + } if (timer->m_Next <= now) { timer->Call();