Browse Source

Fixed a memory leak in the code generators.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/593/head
Dimitar Dobrev 10 years ago
parent
commit
575649b5da
  1. 9
      src/Generator.Tests/Passes/TestPasses.cs
  2. 2
      src/Generator/Driver.cs
  3. 10
      src/Generator/Generator.cs
  4. 6
      src/Generator/Generators/CLI/CLIGenerator.cs
  5. 6
      src/Generator/Generators/CSharp/CSharpGenerator.cs

9
src/Generator.Tests/Passes/TestPasses.cs

@ -70,7 +70,7 @@ namespace CppSharp.Generator.Tests.Passes @@ -70,7 +70,7 @@ namespace CppSharp.Generator.Tests.Passes
[Test]
public void TestCaseRenamePass()
{
Type.TypePrinterDelegate += type => type.Visit(new CSharpTypePrinter(Driver)).Type;
CppSharp.AST.Type.TypePrinterDelegate += TypePrinterDelegate;
var c = AstContext.Class("TestRename");
@ -82,6 +82,8 @@ namespace CppSharp.Generator.Tests.Passes @@ -82,6 +82,8 @@ namespace CppSharp.Generator.Tests.Passes
Assert.That(method.Name, Is.EqualTo("LowerCaseMethod"));
Assert.That(field.Name, Is.EqualTo("LowerCaseField"));
CppSharp.AST.Type.TypePrinterDelegate -= TypePrinterDelegate;
}
[Test]
@ -202,5 +204,10 @@ namespace CppSharp.Generator.Tests.Passes @@ -202,5 +204,10 @@ namespace CppSharp.Generator.Tests.Passes
passBuilder.RunPasses(pass => pass.VisitLibrary(AstContext));
Assert.AreEqual(method.Access , AccessSpecifier.Internal);
}
private string TypePrinterDelegate(CppSharp.AST.Type type)
{
return type.Visit(new CSharpTypePrinter(Driver)).Type;
}
}
}

2
src/Generator/Driver.cs

@ -496,6 +496,8 @@ namespace CppSharp @@ -496,6 +496,8 @@ namespace CppSharp
if (driver.Options.IsCSharpGenerator && driver.Options.CompileCode)
driver.CompileCode();
}
driver.Generator.Dispose();
}
}
}

10
src/Generator/Generator.cs

@ -33,13 +33,14 @@ namespace CppSharp.Generators @@ -33,13 +33,14 @@ namespace CppSharp.Generators
/// <summary>
/// Generators are the base class for each language backend.
/// </summary>
public abstract class Generator
public abstract class Generator : IDisposable
{
public Driver Driver { get; private set; }
protected Generator(Driver driver)
{
Driver = driver;
CppSharp.AST.Type.TypePrinterDelegate += TypePrinterDelegate;
}
/// <summary>
@ -119,9 +120,16 @@ namespace CppSharp.Generators @@ -119,9 +120,16 @@ namespace CppSharp.Generators
/// </summary>
public abstract List<Template> Generate(IEnumerable<TranslationUnit> units);
protected abstract string TypePrinterDelegate(CppSharp.AST.Type type);
public static string GeneratedIdentifier(string id)
{
return "__" + id;
}
public void Dispose()
{
CppSharp.AST.Type.TypePrinterDelegate -= TypePrinterDelegate;
}
}
}

6
src/Generator/Generators/CLI/CLIGenerator.cs

@ -14,7 +14,6 @@ namespace CppSharp.Generators.CLI @@ -14,7 +14,6 @@ namespace CppSharp.Generators.CLI
public CLIGenerator(Driver driver) : base(driver)
{
typePrinter = new CLITypePrinter(driver);
Type.TypePrinterDelegate += type => type.Visit(typePrinter);
}
public override List<Template> Generate(IEnumerable<TranslationUnit> units)
@ -45,5 +44,10 @@ namespace CppSharp.Generators.CLI @@ -45,5 +44,10 @@ namespace CppSharp.Generators.CLI
return false;
return @class.IsRefType && (!@class.HasBase || !@class.HasRefBase());
}
protected override string TypePrinterDelegate(Type type)
{
return type.Visit(typePrinter);
}
}
}

6
src/Generator/Generators/CSharp/CSharpGenerator.cs

@ -13,7 +13,6 @@ namespace CppSharp.Generators.CSharp @@ -13,7 +13,6 @@ namespace CppSharp.Generators.CSharp
{
typePrinter = new CSharpTypePrinter(driver);
expressionPrinter = new CSharpExpressionPrinter(typePrinter);
CppSharp.AST.Type.TypePrinterDelegate += type => type.Visit(typePrinter).Type;
}
public override List<Template> Generate(IEnumerable<TranslationUnit> units)
@ -37,5 +36,10 @@ namespace CppSharp.Generators.CSharp @@ -37,5 +36,10 @@ namespace CppSharp.Generators.CSharp
return true;
}
protected override string TypePrinterDelegate(Type type)
{
return type.Visit(typePrinter).Type;
}
}
}

Loading…
Cancel
Save