Skip to content

Commit

Permalink
Added a fix for a multi-threading bug that occured when TerrainTiles …
Browse files Browse the repository at this point in the history
…were accessed via the Terrain::_updateTerrainTileSet that were being deleted at the same time by the DatabasePager thread.
  • Loading branch information
robertosfield committed Aug 31, 2012
1 parent 2ffa8d8 commit 85ad82a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/osgTerrain/Terrain.cpp
Expand Up @@ -97,7 +97,17 @@ void Terrain::traverse(osg::NodeVisitor& nv)
TerrainTileList tiles;
{
OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_mutex);
std::copy(_updateTerrainTileSet.begin(), _updateTerrainTileSet.end(), std::back_inserter(tiles));
for(TerrainTileSet::iterator itr = _updateTerrainTileSet.begin(); itr !=_updateTerrainTileSet.end(); ++itr)
{
// take a reference first to make sure that the referenceCount can be safely read without another thread decrementing it to zero.
(*itr)->ref();

// only if referenceCount is 2 or more indicating there is still a reference held elsewhere is it safe to add it to list of tiles to be updated
if ((*itr)->referenceCount()>1) tiles.push_back(*itr);

// use unref_nodelete to avoid any issues when the *itr TerrainTile has been deleted by another thread while this for loop has been running.
(*itr)->unref_nodelete();
}
_updateTerrainTileSet.clear();
}

Expand Down

0 comments on commit 85ad82a

Please sign in to comment.