From a9aeb492dc1ca7524d3546e875e331dadd92ab89 Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Thu, 4 Feb 2016 22:21:29 +0000 Subject: [PATCH] Problem: Valgrind reports read of freed memory Solution: when iterating over a map and conditionally deleting elements, an erased iterator gets invalidated. Call erase using postfix increment on iterator to avoid using an invalid element in the next iteration. --- src/radio.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/radio.cpp b/src/radio.cpp index fb27efcc..835d9caa 100644 --- a/src/radio.cpp +++ b/src/radio.cpp @@ -99,9 +99,11 @@ void zmq::radio_t::xwrite_activated (pipe_t *pipe_) void zmq::radio_t::xpipe_terminated (pipe_t *pipe_) { - for (subscriptions_t::iterator it = subscriptions.begin (); it != subscriptions.end (); ++it) { + for (subscriptions_t::iterator it = subscriptions.begin (); it != subscriptions.end (); ) { if (it->second == pipe_) { - subscriptions.erase (it); + subscriptions.erase (it++); + } else { + ++it; } }