@@ -715,13 +715,10 @@ void PeerConnection::iterateDataChannels(
715
715
{
716
716
std::shared_lock lock (mDataChannelsMutex ); // read-only
717
717
locked.reserve (mDataChannels .size ());
718
- auto it = mDataChannels .begin ();
719
- while (it != mDataChannels .end ()) {
718
+ for (auto it = mDataChannels .begin (); it != mDataChannels .end (); ++it) {
720
719
auto channel = it->second .lock ();
721
720
if (channel && !channel->isClosed ())
722
721
locked.push_back (std::move (channel));
723
-
724
- ++it;
725
722
}
726
723
}
727
724
@@ -783,15 +780,22 @@ shared_ptr<Track> PeerConnection::emplaceTrack(Description::Media description) {
783
780
}
784
781
785
782
void PeerConnection::iterateTracks (std::function<void (shared_ptr<Track> track)> func) {
786
- std::shared_lock lock (mTracksMutex ); // read-only
787
- for (auto it = mTrackLines .begin (); it != mTrackLines .end (); ++it) {
788
- auto track = it->lock ();
789
- if (track && !track->isClosed ()) {
790
- try {
791
- func (std::move (track));
792
- } catch (const std::exception &e) {
793
- PLOG_WARNING << e.what ();
794
- }
783
+ std::vector<shared_ptr<Track>> locked;
784
+ {
785
+ std::shared_lock lock (mTracksMutex ); // read-only
786
+ locked.reserve (mTrackLines .size ());
787
+ for (auto it = mTrackLines .begin (); it != mTrackLines .end (); ++it) {
788
+ auto track = it->lock ();
789
+ if (track && !track->isClosed ())
790
+ locked.push_back (std::move (track));
791
+ }
792
+ }
793
+
794
+ for (auto &track : locked) {
795
+ try {
796
+ func (std::move (track));
797
+ } catch (const std::exception &e) {
798
+ PLOG_WARNING << e.what ();
795
799
}
796
800
}
797
801
}
0 commit comments