Browse Source

Merge pull request #109 from ddobrev/master

Added a check for 'null' before getting the native pointer of a wrapped object.
pull/111/head
João Matos 12 years ago
parent
commit
51ddae75fc
  1. 17
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 2
      tests/CSharpTemp/CSharpTemp.Tests.cs
  3. 10
      tests/CSharpTemp/CSharpTemp.cpp
  4. 2
      tests/CSharpTemp/CSharpTemp.h

17
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -572,17 +572,24 @@ namespace CppSharp.Generators.CSharp
return; return;
} }
if (Context.Parameter.Type.IsAddress()) string param = Helpers.SafeIdentifier(Context.Parameter.Name);
Type type = Context.Parameter.Type.Desugar();
if (type.IsAddress())
{ {
Context.Return.Write("{0}.{1}", Helpers.SafeIdentifier(Context.Parameter.Name), Class decl;
Helpers.InstanceIdentifier); if (type.IsTagDecl(out decl) && decl.IsValueType)
Context.Return.Write("{0}.{1}", param, Helpers.InstanceIdentifier);
else
Context.Return.Write("{0} == ({2}) null ? global::System.IntPtr.Zero : {0}.{1}", param,
Helpers.InstanceIdentifier, type);
return; return;
} }
var qualifiedIdentifier = CSharpMarshalNativeToManagedPrinter.QualifiedIdentifier( var qualifiedIdentifier = CSharpMarshalNativeToManagedPrinter.QualifiedIdentifier(
@class.OriginalClass ?? @class); @class.OriginalClass ?? @class);
Context.Return.Write("*({0}.Internal*){1}.{2}", qualifiedIdentifier, Context.Return.Write(
Helpers.SafeIdentifier(Context.Parameter.Name), Helpers.InstanceIdentifier); "{1} == ({0}) null ? new {0}.Internal() : *({0}.Internal*) ({1}.{2})", qualifiedIdentifier,
param, Helpers.InstanceIdentifier);
} }
private void MarshalValueClass(Class @class) private void MarshalValueClass(Class @class)

2
tests/CSharpTemp/CSharpTemp.Tests.cs

@ -33,7 +33,7 @@ public class CSharpTempTests
[Test] [Test]
public void TestFixedArrays() public void TestFixedArrays()
{ {
Qux qux = new Qux(); Qux qux = new Qux(null);
var array = new[] { 1, 2, 3 }; var array = new[] { 1, 2, 3 };
qux.Array = array; qux.Array = array;
for (int i = 0; i < qux.Array.Length; i++) for (int i = 0; i < qux.Array.Length; i++)

10
tests/CSharpTemp/CSharpTemp.cpp

@ -31,6 +31,16 @@ const Foo& Bar::operator[](int i) const
return m_foo; return m_foo;
} }
Qux::Qux()
{
}
Qux::Qux(Foo foo)
{
}
int Qux::farAwayFunc() const int Qux::farAwayFunc() const
{ {
return 20; return 20;

2
tests/CSharpTemp/CSharpTemp.h

@ -21,6 +21,8 @@ protected:
class DLL_API Qux class DLL_API Qux
{ {
public: public:
Qux();
Qux(Foo foo);
int farAwayFunc() const; int farAwayFunc() const;
int array[3]; int array[3];
}; };

Loading…
Cancel
Save