Skip to content

Commit

Permalink
#5408: Fix change tracker registration/deregistration leak when handl…
Browse files Browse the repository at this point in the history
…ing root nodes in the MapResource class.
  • Loading branch information
codereader committed Oct 25, 2021
1 parent b5439e4 commit 2144bcb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
35 changes: 17 additions & 18 deletions radiantcore/map/MapResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ MapResource::MapResource(const std::string& resourcePath)
refreshLastModifiedTime();
}

MapResource::~MapResource()
{
clear();
}

void MapResource::rename(const std::string& fullPath)
{
constructPaths(fullPath);
Expand Down Expand Up @@ -98,8 +103,7 @@ bool MapResource::load()
if (!_mapRoot)
{
// Map not loaded yet, acquire map root node from loader
_mapRoot = loadMapNode();
connectMap();
setRootNode(loadMapNode());
mapSave();
}

Expand Down Expand Up @@ -232,19 +236,23 @@ const scene::IMapRootNodePtr& MapResource::getRootNode()

void MapResource::setRootNode(const scene::IMapRootNodePtr& root)
{
// Unsubscribe from the old root node first
if (_mapRoot)
{
_mapRoot->getUndoChangeTracker().setChangedCallback(std::function<void()>());
}

_mapRoot = root;
}

void MapResource::clear()
{
// Disconnect from the old root first
if (_mapRoot)
{
_mapRoot->getUndoChangeTracker().setChangedCallback(Callback());
_mapRoot->getUndoChangeTracker().setChangedCallback(std::bind(&MapResource::onMapChanged, this));
}
}

_mapRoot = std::make_shared<RootNode>("");
connectMap();
void MapResource::clear()
{
setRootNode(std::make_shared<RootNode>(""));
}

bool MapResource::fileHasBeenModifiedSinceLastSave()
Expand All @@ -259,15 +267,6 @@ void MapResource::onMapChanged()
GlobalMap().setModified(!_mapRoot->getUndoChangeTracker().saved());
}

void MapResource::connectMap()
{
if (_mapRoot)
{
// Reroute the changed callback to the onMapChanged() call.
_mapRoot->getUndoChangeTracker().setChangedCallback(std::bind(&MapResource::onMapChanged, this));
}
}

void MapResource::mapSave()
{
if (_mapRoot)
Expand Down
3 changes: 1 addition & 2 deletions radiantcore/map/MapResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MapResource :
public:
// Constructor
MapResource(const std::string& resourcePath);
virtual ~MapResource();

virtual void rename(const std::string& fullPath) override;

Expand Down Expand Up @@ -85,8 +86,6 @@ class MapResource :

RootNodePtr loadMapNode();

void connectMap();

// Opens a stream for the given path, which might be VFS path or an absolute one.
// Throws IMapResource::OperationException on stream open failure.
stream::MapResourceStream::Ptr openFileStream(const std::string& path);
Expand Down

0 comments on commit 2144bcb

Please sign in to comment.