Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Clear RenderGeoJSONSource tilePyramid if no data is available - #9883

Merged
brunoabinader merged 1 commit into
masterfrom
geojson-data-is-fresh
Aug 30, 2017
Merged

brunoabinader merged 1 commit into
masterfrom
geojson-data-is-fresh

Conversation

@brunoabinader

Copy link
Copy Markdown
Contributor

In some circumstances, two different objects might get the same memory address if the former has been deleted before the latter gets constructed. Thus, we cannot rely solely on pointer comparsion when checking whether to update the tile data in RenderGeoJSONSource::tylePiramid.

Fixes #9868.

@brunoabinader brunoabinader added bug Core The cross-platform C++ core, aka mbgl labels Aug 29, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we're already checking needsUpdate, we can remove the pointer comparison altogether.

@brunoabinader
brunoabinader force-pushed the geojson-data-is-fresh branch 2 times, most recently from c75552d to b62041d Compare August 29, 2017 12:14
@jfirebaugh

jfirebaugh commented Aug 29, 2017 •

Copy link
Copy Markdown
Contributor

Are you sure this is the issue? I considered this possibility when I wrote this code, and concluded it wasn't possible:

  • GeoJSONData is owned by GeoJSONSource::Impl
  • GeoJSONSource::Impl is held in an Immutable
  • RenderGeoJSONSource shared ownership of an Immutable<GeoJSONSource::Impl> (in the base RenderSource)
  • In RenderGeoJSONSource::update, the baseImpls are swapped at the beginning of the function, so it's possible that at the time of the comparison, data longer valid points to a valid location. But we can say that when RenderGeoJSONSource::update begins execution:
    • data is live, because it's a pointer to the same data in the ownership chain above, which was live at that point
    • and obviously baseImpl_ and its data_ is live since it's a function parameter.
    • → Therefore both objects were live at one point, so their pointers can't be equal

@brunoabinader

brunoabinader commented Aug 29, 2017 •

Copy link
Copy Markdown
Contributor Author

OK, let's get back to the following assumptions:

  1. The test fails when the map object already contains a style object, including its sources and layers.
  2. Whenever parsing a style, Style::Impl clears all sources and layers
  3. When a source is removed, we don't immediately delete its impl if there is a reference for it in a render source created on a previous render step.

This guarantees the pointer comparison arithmetic would be valid by following the ownership chain described in #9883 (comment).

However, there is an exception:

When data_ is null (which occurs when GeoJSONSource is created, but the file source lambda in GeoJSONSource::loadDescription has not yet finished fetching the source), we early return without updating data - which might now point to a non-living source data. In the next source update, if the new source data has the same memory address as the old one, then 💥 (see printf backtrace below):

[0x101e174c8] GeoJSONSource::Impl::Impl
[0x101e167e0] RenderGeoJSONSource::update (old data: 0x101e18ac0 new data: 0x0)
[0x101e19918] GeoJSONSource::Impl::~Impl
[0x101e18ac0] GeoJSONData::~GeoJSONData
[0x101e17f70] GeoJSONSource::operator() (parsed baseImpl)
[0x101e1f958] GeoJSONSource::Impl::Impl (other: 0x101e174c8)
[0x101e18ac0] GeoJSONData::GeoJSONData
[0x101e167e0] RenderGeoJSONSource::update (old data: 0x101e18ac0 new data: 0x101e18ac0)
[0x101e174c8] GeoJSONSource::Impl::~Impl
* failed geojson external-linestring

In sum: we could also solve this problem by always updating data before checking whether it is valid or not and early returning.

@jfirebaugh

Copy link
Copy Markdown
Contributor

Ahh, good analysis -- I hadn't considered that possibility.

In sum: we could also solve this problem by always updating data before checking whether it is valid or not and early returning.

Yeah, that sounds like the correct approach. Corrected version would be something like:

    if (data_ != data) {
        data = data_;
        tilePyramid.cache.clear();

        if (data) {
            for (auto const& item : tilePyramid.tiles) {
                static_cast<GeoJSONTile*>(item.second.get())->updateData(data->getTile(item.first.canonical));
            }
        } else {
            // clear existing tiles
        }
    }

@brunoabinader
brunoabinader force-pushed the geojson-data-is-fresh branch from b62041d to 9bbb4ae Compare August 30, 2017 10:48
@brunoabinader brunoabinader changed the title Don't rely on pointer comparison in RenderGeoJSONSource when updating its tiles data Clear RenderGeoJSONSource tilePyramid if no data is available Aug 30, 2017
@brunoabinader
brunoabinader force-pushed the geojson-data-is-fresh branch from 9bbb4ae to 0466f3b Compare August 30, 2017 11:04
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug Core The cross-platform C++ core, aka mbgl

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants