diff --git a/test/src/mbgl/test/stub_file_source.cpp b/test/src/mbgl/test/stub_file_source.cpp index 75d7e2d07..ec0545e88 100644 --- a/test/src/mbgl/test/stub_file_source.cpp +++ b/test/src/mbgl/test/stub_file_source.cpp @@ -24,11 +24,19 @@ StubFileSource::StubFileSource() { for (auto& pair : pending_) { optional res = std::get<1>(pair.second)(std::get<0>(pair.second)); if (res) { - std::get<2>(pair.second)(*res); - + // This must be before calling the callback, because it's possible that the callback + // could: + // + // 1. Deallocate the AsyncRequest itself, thus removing it from pending + // 2. Allocate a new AsyncRequest at the same memory location + // + // If remove(pair.first) was called after both those things happened, it would + // remove the newly allocated request rather than the intended request. if (!res->error) { remove(pair.first); } + + std::get<2>(pair.second)(*res); } } });