connection_handler_impl: Don't re-use erased STL iterator
authorBrendan Shanks <bshanks@audiovox.com>
Fri, 21 Mar 2014 03:41:36 +0000 (20:41 -0700)
committerJustin Dickow <jjdickow@gmail.com>
Fri, 21 Mar 2014 12:53:38 +0000 (08:53 -0400)
Reusing an STL vector iterator (like advancing it) after erasing it doesn't work. Assign itr to the result of erase() instead.

This was causing a segfault on Mac when the phone would disconnect

SDL_Core/src/components/connection_handler/src/connection_handler_impl.cc

index 76e3739..995526f 100644 (file)
@@ -90,8 +90,9 @@ void ConnectionHandlerImpl::OnDeviceListUpdated(
   LOG4CXX_INFO(logger_, "ConnectionHandlerImpl::OnDeviceListUpdated()");
 
   bool list_actually_changed = false;
-  for (DeviceListIterator itr = device_list_.begin(); itr != device_list_.end();
-       ++itr) {
+
+  DeviceListIterator itr = device_list_.begin();
+  while (itr != device_list_.end()) {
     if (!DoesDeviceExistInTMList(device_info_list, (*itr).first)) {
       // Device has been removed. Perform all needed actions.
       // 1. Delete all the connections and sessions of this device
@@ -105,12 +106,15 @@ void ConnectionHandlerImpl::OnDeviceListUpdated(
           RemoveConnection((*it).first);
         }
       }
-      device_list_.erase(device_for_remove_handle);
+      itr = device_list_.erase(itr);
       list_actually_changed = true;
       if (connection_handler_observer_) {
         connection_handler_observer_->RemoveDevice(device_for_remove_handle);
       }
     }
+    else {
+      ++itr;
+    }
   }
   for (std::vector<transport_manager::DeviceInfo>::const_iterator it_in =
          device_info_list.begin(); it_in != device_info_list.end(); ++it_in) {