From: Gunnar Beutner Date: Mon, 1 Feb 2016 08:33:33 +0000 (+0100) Subject: Avoid unnecessary new/deletes X-Git-Tag: v2.5.0~570 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c03e458e134e8ed9468bcef555151007ec47c358;p=icinga2 Avoid unnecessary new/deletes refs #11014 --- diff --git a/lib/base/socketevents.cpp b/lib/base/socketevents.cpp index 5a99a5e04..fb739011e 100644 --- a/lib/base/socketevents.cpp +++ b/lib/base/socketevents.cpp @@ -80,18 +80,16 @@ void SocketEvents::ThreadProc(void) { Utility::SetThreadName("SocketIO"); - pollfd *pfds = NULL; - SocketEventDescriptor *descriptors = NULL; - int pfdcount; + std::vector pfds; + std::vector descriptors; for (;;) { { boost::mutex::scoped_lock lock(l_SocketIOMutex); - if (pfds == NULL) { - pfdcount = l_SocketIOSockets.size(); - pfds = new pollfd[pfdcount]; - descriptors = new SocketEventDescriptor[pfdcount]; + if (l_SocketIOFDChanged) { + pfds.resize(l_SocketIOSockets.size()); + descriptors.resize(l_SocketIOSockets.size()); int i = 0; @@ -111,10 +109,12 @@ void SocketEvents::ThreadProc(void) } } + ASSERT(!pfds.empty()); + #ifdef _WIN32 - (void) WSAPoll(pfds, pfdcount, -1); + (void) WSAPoll(&pfds[0], pfds.size(), -1); #else /* _WIN32 */ - (void) poll(pfds, pfdcount, -1); + (void) poll(&pfds[0], pfds.size(), -1); #endif /* _WIN32 */ std::vector events; @@ -122,14 +122,10 @@ void SocketEvents::ThreadProc(void) { boost::mutex::scoped_lock lock(l_SocketIOMutex); - if (l_SocketIOFDChanged) { - delete [] pfds; - delete [] descriptors; - pfds = NULL; + if (l_SocketIOFDChanged) continue; - } - for (int i = 0; i < pfdcount; i++) { + for (int i = 0; i < pfds.size(); i++) { if ((pfds[i].revents & (POLLIN | POLLOUT | POLLHUP | POLLERR)) == 0) continue;