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.
This commit is contained in:
Luca Boccassi 2016-02-04 22:21:29 +00:00
parent c5bf0dc0a4
commit a9aeb492dc

View File

@ -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;
}
}