Fixed IDisposable implementation and usage.

- Implement IDisposable interface correctly, with sealed classes where possible for simplicity.
- Add using statement around undisposed local variables.
This commit is contained in:
RoosterDragon
2014-05-21 06:19:26 +01:00
parent 334a210231
commit a598a01108
37 changed files with 248 additions and 260 deletions

View File

@@ -15,7 +15,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class CallLuaFunc : Activity, IDisposable
public sealed class CallLuaFunc : Activity, IDisposable
{
LuaFunction function;
@@ -39,28 +39,11 @@ namespace OpenRA.Mods.RA.Activities
base.Cancel(self);
}
protected void Dispose(bool disposing)
{
if (function == null)
return;
if (disposing)
{
function.Dispose();
function = null;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~CallLuaFunc()
{
// Dispose unmanaged resources only
Dispose(false);
if (function == null) return;
function.Dispose();
function = null;
}
}
}

View File

@@ -8,6 +8,7 @@
*/
#endregion
using System;
using OpenRA.Graphics;
using OpenRA.Scripting;
using OpenRA.Traits;
@@ -21,7 +22,7 @@ namespace OpenRA.Mods.RA.Scripting
public object Create(ActorInitializer init) { return new LuaScript(this); }
}
public class LuaScript : ITick, IWorldLoaded
public sealed class LuaScript : ITick, IWorldLoaded, IDisposable
{
readonly LuaScriptInfo info;
ScriptContext context;
@@ -42,5 +43,11 @@ namespace OpenRA.Mods.RA.Scripting
{
context.Tick(self);
}
public void Dispose()
{
if (context != null)
context.Dispose();
}
}
}

View File

@@ -18,7 +18,7 @@ using OpenRA.Primitives;
namespace OpenRA.Mods.RA.Scripting
{
public class LuaScriptContext : IDisposable
public sealed class LuaScriptContext : IDisposable
{
public Lua Lua { get; private set; }
readonly Cache<string, LuaFunction> functionCache;
@@ -133,19 +133,9 @@ namespace OpenRA.Mods.RA.Scripting
}
public void Dispose()
{
if (Lua == null)
return;
GC.SuppressFinalize(this);
Lua.Dispose();
Lua = null;
}
~LuaScriptContext()
{
if (Lua != null)
Game.RunAfterTick(Dispose);
Lua.Dispose();
}
}
}

View File

@@ -32,7 +32,7 @@ namespace OpenRA.Mods.RA.Scripting
public object Create(ActorInitializer init) { return new LuaScriptInterface(this); }
}
public class LuaScriptInterface : IWorldLoaded, ITick
public sealed class LuaScriptInterface : IWorldLoaded, ITick, IDisposable
{
World world;
SpawnMapActors sma;
@@ -90,6 +90,11 @@ namespace OpenRA.Mods.RA.Scripting
context.InvokeLuaFunction("Tick");
}
public void Dispose()
{
context.Dispose();
}
[LuaGlobal]
public object New(string typeName, LuaTable args)
{

View File

@@ -21,7 +21,7 @@ namespace OpenRA.Mods.RA.Scripting
[Desc("Allows map scripts to attach triggers to this actor via the Triggers global.")]
public class ScriptTriggersInfo : TraitInfo<ScriptTriggers> { }
public class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, IDisposable
public sealed class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, IDisposable
{
public event Action<Actor> OnKilledInternal = _ => {};
@@ -100,37 +100,11 @@ namespace OpenRA.Mods.RA.Scripting
}
}
bool disposed;
protected void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
var toDispose = new [] { onIdle, onDamaged, onKilled, onProduction };
foreach (var f in toDispose.SelectMany(f => f))
f.First.Dispose();
foreach (var l in toDispose)
l.Clear();
}
disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~ScriptTriggers()
{
// Dispose unmanaged resources only
Dispose(false);
var pairs = new[] { onIdle, onDamaged, onKilled, onProduction };
pairs.SelectMany(l => l).Select(p => p.First).Do(f => f.Dispose());
pairs.Do(l => l.Clear());
}
}
}