From 5511e9a4e85c8eb8a67a8cfc56a2b1f665a8940d Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 22 Jul 2016 15:13:57 -0700 Subject: [PATCH] [tests] Fix a subtle ordering bug in StubFileSource --- test/src/mbgl/test/stub_file_source.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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); } } });