Ticket #4642: invalidated-iterator

File invalidated-iterator, 1.2 KB (added by Erik Hovland <erik@…>, 13 years ago)

changes do loop for for loop and attempts to keep iterator valid

Line 
1List iterators can become invalidated by executing on them.
2
3From: Erik Hovland <erik@hovland.org>
4
5If they are then used in testing they may behave correctly. But
6it is likely that the behavior is undefined and the invalidated
7iterator can actually cause a segfault if accessed.
8
9This patch addresses an instance where a iterator is invalidated
10and is then tested.
11---
12
13 libs/libmythtv/firewiredevice.cpp |    7 +++----
14 1 files changed, 3 insertions(+), 4 deletions(-)
15
16diff --git a/libs/libmythtv/firewiredevice.cpp b/libs/libmythtv/firewiredevice.cpp
17index 52f7f9c..8abda84 100644
18--- a/libs/libmythtv/firewiredevice.cpp
19+++ b/libs/libmythtv/firewiredevice.cpp
20@@ -49,13 +49,12 @@ void FirewireDevice::RemoveListener(TSDataListener *listener)
21 {
22     vector<TSDataListener*>::iterator it = m_listeners.end();
23 
24-    do
25+    for (it = find(m_listeners.begin(), m_listeners.end(), listener);
26+         it != m_listeners.end();)
27     {
28+        m_listeners.erase(it);
29         it = find(m_listeners.begin(), m_listeners.end(), listener);
30-        if (it != m_listeners.end())
31-            m_listeners.erase(it);
32     }
33-    while (it != m_listeners.end());
34 
35     VERBOSE(VB_RECORD, LOC + "RemoveListener() "<<m_listeners.size());
36 }