Browse Source

Removed the managed calls to destructors of std::strings passed by value.

The native code calls the destructors itself.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/696/head
Dimitar Dobrev 9 years ago
parent
commit
cbafb55044
  1. 21
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 9
      src/Generator/Types/Std/Stdlib.cs
  3. 15
      tests/Common/Common.Tests.cs

21
src/Generator/Generators/CSharp/CSharpSources.cs

@ -1601,7 +1601,7 @@ namespace CppSharp.Generators.CSharp @@ -1601,7 +1601,7 @@ namespace CppSharp.Generators.CSharp
{
if (method.IsDestructor)
{
WriteLine("{0}.Dispose(false);", Helpers.TargetIdentifier);
WriteLine("{0}.Dispose(true);", Helpers.TargetIdentifier);
return;
}
@ -1930,15 +1930,9 @@ namespace CppSharp.Generators.CSharp @@ -1930,15 +1930,9 @@ namespace CppSharp.Generators.CSharp
// Generate Dispose(bool) method
PushBlock(CSharpBlockKind.Method);
if (@class.IsValueType)
{
Write("private ");
}
else
{
Write("protected ");
Write("public ");
if (!@class.IsValueType)
Write(hasBaseClass ? "override " : "virtual ");
}
WriteLine("void Dispose(bool disposing)");
WriteStartBraceIndent();
@ -1978,8 +1972,10 @@ namespace CppSharp.Generators.CSharp @@ -1978,8 +1972,10 @@ namespace CppSharp.Generators.CSharp
if (!Options.CheckSymbols ||
Driver.Symbols.FindLibraryBySymbol(dtor.Mangled, out library))
{
WriteLine("if (disposing)");
if (dtor.IsVirtual)
{
WriteStartBraceIndent();
GenerateVirtualFunctionCall(dtor, @class, true);
if (@class.IsAbstract)
{
@ -1987,11 +1983,16 @@ namespace CppSharp.Generators.CSharp @@ -1987,11 +1983,16 @@ namespace CppSharp.Generators.CSharp
WriteLine("else");
PushIndent();
GenerateInternalFunctionCall(dtor);
PopIndent();
PopIndent();
}
WriteCloseBraceIndent();
}
else
{
PushIndent();
GenerateInternalFunctionCall(dtor);
PopIndent();
}
}
}

9
src/Generator/Types/Std/Stdlib.cs

@ -78,14 +78,15 @@ namespace CppSharp.Types.Std @@ -78,14 +78,15 @@ namespace CppSharp.Types.Std
{
string varAllocator = "__allocator" + ctx.ParameterIndex;
string varBasicString = "__basicString" + ctx.ParameterIndex;
ctx.SupportBefore.WriteLine("using (var {0} = new {1}())",
ctx.SupportBefore.WriteLine("var {0} = new {1}();",
varAllocator, allocator.Visit(typePrinter));
ctx.SupportBefore.WriteLine("using (var {0} = new {1}({2}, {3}))",
ctx.SupportBefore.WriteLine("var {0} = new {1}({2}, {3});",
varBasicString, basicString.Visit(typePrinter),
ctx.Parameter.Name, varAllocator);
ctx.SupportBefore.WriteStartBraceIndent();
ctx.Return.Write("{0}.{1}", varBasicString, Helpers.InstanceIdentifier);
ctx.HasCodeBlock = true;
ctx.Cleanup.WriteLine("{0}.Dispose({1});", varBasicString,
type.IsPointer() ? "true" : "false");
ctx.Cleanup.WriteLine("{0}.Dispose();", varAllocator);
}
}

15
tests/Common/Common.Tests.cs

@ -636,11 +636,20 @@ public class CommonTests : GeneratorTestFixture @@ -636,11 +636,20 @@ public class CommonTests : GeneratorTestFixture
[Test, Ignore("We need symbols for std::string to invoke and auto-compilation of exported templates is not added yet.")]
public void TestStdString()
{
// when C++ memory is deleted, it's only marked as free but not immediadely freed
// this can hide memory bugs while marshalling
// so let's use a long string to increase the chance of a crash right away
const string t = @"This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.
This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.
This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.
This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.
This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.
This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string. This is a very long string.";
using (var hasStdString = new HasStdString())
{
Assert.That(hasStdString.testStdString("test"), Is.EqualTo("test_test"));
hasStdString.s = "test";
Assert.That(hasStdString.s, Is.EqualTo("test"));
Assert.That(hasStdString.testStdString(t), Is.EqualTo(t + "_test"));
hasStdString.s = t;
Assert.That(hasStdString.s, Is.EqualTo(t));
}
}

Loading…
Cancel
Save