Fix VMAP probe handle leaks and tile model ownership
Close the tile stream opened during existence checks and validate parent-map fallback directly. Track each tile's successful model acquisitions so failed or partial loads cannot release references owned by dynamic objects or other tiles. Add production-method regression tests covering file descriptor stability, transient acquisition failures, shared references, duplicate loads, partial files and parent-map overlaps. Reproduced both original defects on 6f8f59ec6d; build and test-server verification passed.
This commit is contained in:
1 parent
6f8f59ec6d
commit
fe03ea8ca5
3 files changed
+206
-75
No files matched your search
@@ -285,19 +285,9 @@ namespace VMAP
|
||||
fclose(rf);
|
||||
return LoadResult::FileNotFound;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string tilefile = basePath + getTileFileName(mapID, tileX, tileY);
|
||||
FILE* tf = fopen(tilefile.c_str(), "rb");
|
||||
if (!tf)
|
||||
result = LoadResult::FileNotFound;
|
||||
else
|
||||
{
|
||||
if (!readChunk(tf, chunk, VMAP_MAGIC, 8))
|
||||
result = LoadResult::VersionMismatch;
|
||||
fclose(tf);
|
||||
}
|
||||
}
|
||||
if (!readChunk(tf, chunk, VMAP_MAGIC, 8))
|
||||
result = LoadResult::VersionMismatch;
|
||||
fclose(tf);
|
||||
fclose(rf);
|
||||
return result;
|
||||
}
|
||||
@@ -349,15 +339,13 @@ namespace VMAP
|
||||
|
||||
void StaticMapTree::UnloadMap(VMapManager2* vm)
|
||||
{
|
||||
for (loadedSpawnMap::iterator i = iLoadedSpawns.begin(); i != iLoadedSpawns.end(); ++i)
|
||||
while (!iLoadedTiles.empty())
|
||||
{
|
||||
for (uint32 refCount = 0; refCount < i->second; ++refCount)
|
||||
vm->releaseModelInstance(iTreeValues[i->first].getWorldModel()->GetName());
|
||||
|
||||
iTreeValues[i->first].setUnloaded();
|
||||
uint32 tileX, tileY;
|
||||
unpackTileID(iLoadedTiles.begin()->first, tileX, tileY);
|
||||
UnloadMapTile(tileX, tileY, vm);
|
||||
}
|
||||
iLoadedSpawns.clear();
|
||||
iLoadedTiles.clear();
|
||||
}
|
||||
|
||||
//=========================================================
|
||||
@@ -369,7 +357,13 @@ namespace VMAP
|
||||
TC_LOG_ERROR("misc", "StaticMapTree::LoadMapTile() : tree has not been initialized [%u, %u]", tileX, tileY);
|
||||
return LoadResult::ReadFromFileFailed;
|
||||
}
|
||||
uint32 tileID = packTileID(tileX, tileY);
|
||||
auto loaded = iLoadedTiles.find(tileID);
|
||||
if (loaded != iLoadedTiles.end())
|
||||
return loaded->second.Result;
|
||||
|
||||
LoadResult result = LoadResult::FileNotFound;
|
||||
LoadedTile tile;
|
||||
|
||||
TileFileOpenResult fileResult = OpenMapTileFile(iBasePath, iMapID, tileX, tileY, vm);
|
||||
if (fileResult.File)
|
||||
@@ -388,29 +382,38 @@ namespace VMAP
|
||||
ModelSpawn spawn;
|
||||
if (ModelSpawn::readFromFile(fileResult.File, spawn))
|
||||
{
|
||||
// acquire model instance
|
||||
WorldModel* model = vm->acquireModelInstance(iBasePath, spawn.name, spawn.flags);
|
||||
if (!model)
|
||||
TC_LOG_ERROR("misc", "StaticMapTree::LoadMapTile() : could not acquire WorldModel pointer [%u, %u]", tileX, tileY);
|
||||
|
||||
// update tree
|
||||
auto spawnIndex = iSpawnIndices.find(spawn.ID);
|
||||
if (spawnIndex != iSpawnIndices.end())
|
||||
{
|
||||
uint32 referencedVal = spawnIndex->second;
|
||||
if (referencedVal >= iNTreeValues)
|
||||
{
|
||||
TC_LOG_ERROR("maps", "StaticMapTree::LoadMapTile() : invalid tree element (%u/%u) referenced in tile %s", referencedVal, iNTreeValues, fileResult.Name.c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
WorldModel* model = vm->acquireModelInstance(iBasePath, spawn.name, spawn.flags);
|
||||
if (!model)
|
||||
{
|
||||
TC_LOG_ERROR("misc", "StaticMapTree::LoadMapTile() : could not acquire WorldModel pointer [%u, %u]", tileX, tileY);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!iLoadedSpawns.count(referencedVal))
|
||||
{
|
||||
if (referencedVal >= iNTreeValues)
|
||||
{
|
||||
TC_LOG_ERROR("maps", "StaticMapTree::LoadMapTile() : invalid tree element (%u/%u) referenced in tile %s", referencedVal, iNTreeValues, fileResult.Name.c_str());
|
||||
continue;
|
||||
}
|
||||
|
||||
iTreeValues[referencedVal] = ModelInstance(spawn, model);
|
||||
iLoadedSpawns[referencedVal] = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (iTreeValues[referencedVal].getWorldModel() != model)
|
||||
{
|
||||
TC_LOG_ERROR("maps", "StaticMapTree::LoadMapTile() : conflicting model for spawn %u in tile %s", spawn.ID, fileResult.Name.c_str());
|
||||
vm->releaseModelInstance(spawn.name);
|
||||
result = LoadResult::ReadFromFileFailed;
|
||||
continue;
|
||||
}
|
||||
++iLoadedSpawns[referencedVal];
|
||||
#ifdef VMAP_DEBUG
|
||||
if (iTreeValues[referencedVal].ID != spawn.ID)
|
||||
@@ -419,6 +422,7 @@ namespace VMAP
|
||||
TC_LOG_DEBUG("maps", "StaticMapTree::LoadMapTile() : name collision on GUID=%u", spawn.ID);
|
||||
#endif
|
||||
}
|
||||
tile.Models.push_back({ std::move(spawn.name), referencedVal });
|
||||
}
|
||||
else if (int32(iMapID) == fileResult.UsedMapId)
|
||||
{
|
||||
@@ -435,11 +439,10 @@ namespace VMAP
|
||||
result = LoadResult::ReadFromFileFailed;
|
||||
}
|
||||
}
|
||||
iLoadedTiles[packTileID(tileX, tileY)] = true;
|
||||
fclose(fileResult.File);
|
||||
}
|
||||
else
|
||||
iLoadedTiles[packTileID(tileX, tileY)] = false;
|
||||
tile.Result = result;
|
||||
iLoadedTiles.emplace(tileID, std::move(tile));
|
||||
TC_METRIC_EVENT("map_events", "LoadMapTile",
|
||||
"Map: " + std::to_string(iMapID) + " TileX: " + std::to_string(tileX) + " TileY: " + std::to_string(tileY));
|
||||
return result;
|
||||
@@ -456,49 +459,17 @@ namespace VMAP
|
||||
VMAP_ERROR_LOG("misc", "StaticMapTree::UnloadMapTile() : trying to unload non-loaded tile - Map:%u X:%u Y:%u", iMapID, tileX, tileY);
|
||||
return;
|
||||
}
|
||||
if (tile->second) // file associated with tile
|
||||
for (LoadedModelReference const& model : tile->second.Models)
|
||||
{
|
||||
TileFileOpenResult fileResult = OpenMapTileFile(iBasePath, iMapID, tileX, tileY, vm);
|
||||
if (fileResult.File)
|
||||
auto referencedNode = iLoadedSpawns.find(model.TreeIndex);
|
||||
if (referencedNode == iLoadedSpawns.end())
|
||||
VMAP_ERROR_LOG("misc", "StaticMapTree::UnloadMapTile() : trying to unload non-referenced model '%s'", model.Name.c_str());
|
||||
else if (--referencedNode->second == 0)
|
||||
{
|
||||
bool result=true;
|
||||
char chunk[8];
|
||||
if (!readChunk(fileResult.File, chunk, VMAP_MAGIC, 8))
|
||||
result = false;
|
||||
uint32 numSpawns;
|
||||
if (fread(&numSpawns, sizeof(uint32), 1, fileResult.File) != 1)
|
||||
result = false;
|
||||
for (uint32 i=0; i<numSpawns && result; ++i)
|
||||
{
|
||||
// read model spawns
|
||||
ModelSpawn spawn;
|
||||
result = ModelSpawn::readFromFile(fileResult.File, spawn);
|
||||
if (result)
|
||||
{
|
||||
// release model instance
|
||||
vm->releaseModelInstance(spawn.name);
|
||||
|
||||
// update tree
|
||||
auto spawnIndex = iSpawnIndices.find(spawn.ID);
|
||||
if (spawnIndex == iSpawnIndices.end())
|
||||
result = false;
|
||||
else
|
||||
{
|
||||
uint32 referencedNode = spawnIndex->second;
|
||||
if (!iLoadedSpawns.count(referencedNode))
|
||||
VMAP_ERROR_LOG("misc", "StaticMapTree::UnloadMapTile() : trying to unload non-referenced model '%s' (ID:%u)", spawn.name.c_str(), spawn.ID);
|
||||
else if (--iLoadedSpawns[referencedNode] == 0)
|
||||
{
|
||||
iTreeValues[referencedNode].setUnloaded();
|
||||
iLoadedSpawns.erase(referencedNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (int32(iMapID) == fileResult.UsedMapId) // logic documented in StaticMapTree::LoadMapTile
|
||||
result = false;
|
||||
}
|
||||
fclose(fileResult.File);
|
||||
iTreeValues[model.TreeIndex].setUnloaded();
|
||||
iLoadedSpawns.erase(referencedNode);
|
||||
}
|
||||
vm->releaseModelInstance(model.Name);
|
||||
}
|
||||
iLoadedTiles.erase(tile);
|
||||
TC_METRIC_EVENT("map_events", "UnloadMapTile",
|
||||
|
||||
@@ -48,7 +48,17 @@ namespace VMAP
|
||||
|
||||
class TC_COMMON_API StaticMapTree
|
||||
{
|
||||
typedef std::unordered_map<uint32, bool> loadedTileMap;
|
||||
struct LoadedModelReference
|
||||
{
|
||||
std::string Name;
|
||||
uint32 TreeIndex;
|
||||
};
|
||||
struct LoadedTile
|
||||
{
|
||||
LoadResult Result;
|
||||
std::vector<LoadedModelReference> Models;
|
||||
};
|
||||
typedef std::unordered_map<uint32, LoadedTile> loadedTileMap;
|
||||
typedef std::unordered_map<uint32, uint32> loadedSpawnMap;
|
||||
private:
|
||||
uint32 iMapID;
|
||||
@@ -59,7 +69,8 @@ namespace VMAP
|
||||
|
||||
// Store all the map tile idents that are loaded for that map
|
||||
// some maps are not splitted into tiles and we have to make sure, not removing the map before all tiles are removed
|
||||
// empty tiles have no tile file, hence map with bool instead of just a set (consistency check)
|
||||
// Keep precisely the references acquired by each tile. A failed load
|
||||
// must never release another tile's or a dynamic object's model.
|
||||
loadedTileMap iLoadedTiles;
|
||||
std::vector<std::pair<int32, int32>> iLoadedPrimaryTiles;
|
||||
// stores <tree_index, reference_count> to invalidate tree values, unload map, and to be able to report errors
|
||||
|
||||
Reference in new issue
Block a user