From 0b779eaa8b96a65d5b97df7ad468e0380f50703f Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 9 Apr 2015 11:10:51 -0700 Subject: [PATCH] Remove unnecessary use of weak_ptr The request callback runs in the same thread as ~TileData, and is not run if the request is cancelled. So either ~TileData runs first and cancels the request, or the callback runs first, before TileData is destructed. Therefore the weak pointer and lock() check is unnecessary; `this` can be used instead. --- src/mbgl/map/tile_data.cpp | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/mbgl/map/tile_data.cpp b/src/mbgl/map/tile_data.cpp index fdec75993..74993ff52 100644 --- a/src/mbgl/map/tile_data.cpp +++ b/src/mbgl/map/tile_data.cpp @@ -20,9 +20,7 @@ TileData::TileData(const TileID& id_, const SourceInfo& source_) } TileData::~TileData() { - if (req) { - env.cancelRequest(req); - } + cancel(); } const std::string TileData::toString() const { @@ -33,28 +31,19 @@ void TileData::request(uv::worker &worker, float pixelRatio, std::function weak_tile = shared_from_this(); - req = env.request({ Resource::Kind::Tile, url }, [weak_tile, url, callback, &worker](const Response &res) { - util::ptr tile = weak_tile.lock(); - if (!tile || tile->state == State::obsolete) { - // noop. Tile is obsolete and we're now just waiting for the refcount - // to drop to zero for destruction. + req = env.request({ Resource::Kind::Tile, url }, [url, callback, &worker, this](const Response &res) { + req = nullptr; + + if (res.status != Response::Successful) { + Log::Error(Event::HttpRequest, "[%s] tile loading failed: %s", url.c_str(), res.message.c_str()); return; } - // Clear the request object. - tile->req = nullptr; + state = State::loaded; + data = res.data; - if (res.status == Response::Successful) { - tile->state = State::loaded; - - tile->data = res.data; - - // Schedule tile parsing in another thread - tile->reparse(worker, callback); - } else { - Log::Error(Event::HttpRequest, "[%s] tile loading failed: %s", url.c_str(), res.message.c_str()); - } + // Schedule tile parsing in another thread + reparse(worker, callback); }); }