diff --git a/src/impl/realm_coordinator.cpp b/src/impl/realm_coordinator.cpp index 5b60ab4b..4425d2d3 100644 --- a/src/impl/realm_coordinator.cpp +++ b/src/impl/realm_coordinator.cpp @@ -331,8 +331,6 @@ void RealmCoordinator::run_async_notifiers() change_info.resize(1); } else { - change_info.resize(2); - // Sort newly added notifiers by their source version so that we can pull them // all forward to the latest version in a single pass over the transaction log std::sort(new_notifiers.begin(), new_notifiers.end(), @@ -340,6 +338,18 @@ void RealmCoordinator::run_async_notifiers() version = m_advancer_sg->get_version_of_current_transaction(); REALM_ASSERT(version == new_notifiers.front()->version()); + // Preallocate the required amount of space in the vector so that we can + // safely give out pointers to within the vector + { + size_t count = 2; + for (auto it = new_notifiers.begin(), next = it + 1; next != new_notifiers.end(); ++it, ++next) { + if ((*it)->version() != (*next)->version()) + ++count; + } + change_info.reserve(count); + change_info.resize(2); + } + TransactionChangeInfo* info = &change_info.back(); // Advance each of the new notifiers to the latest version, attaching them