Fixed pooling of layers used for pathfinding.

The previous implementation:
- Was failing to dispose of pooled layers.
- Was using a finalizer to allow undisposed layers to be reused.

This means all pooled layers are kept alive indefinitely until the map changes. If the finalizer is slow for any reason then the pathfiinder will allocate new layers when the pool runs out. Since these new layers are eventually stuffed back into the pool when the finalizer does run, this can theoretically leak unbounded memory until the pool goes out of scope. In practice it would leak tens of megabytes.

The new implementation ensures layers are disposed and pooled correctly to allow proper memory reuse. It also introduces some safeguards against memory leaks:
- A cap is set on the number of pooled layers. If more concurrent layers are needed than this, then the excess layers will not be pooled but instead be allowed to be garbage collected.
- No finalizer. An implementation that fails to call dispose simply allows the layer to be garbage collected instead.
This commit is contained in:
RoosterDragon
2015-08-13 23:38:23 +01:00
parent 59edf85513
commit 519be4374c
11 changed files with 158 additions and 170 deletions

View File

@@ -46,9 +46,14 @@ namespace OpenRA.Mods.Common.Activities
moveDisablers = self.TraitsImplementing<IDisableMove>().ToArray();
getPath = () =>
self.World.WorldActor.Trait<IPathFinder>().FindPath(
{
List<CPos> path;
using (var search =
PathSearch.FromPoint(self.World, mobile.Info, self, mobile.ToCell, destination, false)
.WithoutLaneBias());
.WithoutLaneBias())
path = self.World.WorldActor.Trait<IPathFinder>().FindPath(search);
return path;
};
this.destination = destination;
this.nearEnough = WDist.Zero;
}
@@ -85,9 +90,14 @@ namespace OpenRA.Mods.Common.Activities
moveDisablers = self.TraitsImplementing<IDisableMove>().ToArray();
getPath = () =>
self.World.WorldActor.Trait<IPathFinder>().FindPath(
{
List<CPos> path;
using (var search =
PathSearch.FromPoint(self.World, mobile.Info, self, mobile.ToCell, destination, false)
.WithIgnoredActor(ignoredActor));
.WithIgnoredActor(ignoredActor))
path = self.World.WorldActor.Trait<IPathFinder>().FindPath(search);
return path;
};
this.destination = destination;
this.nearEnough = WDist.Zero;