Skip to content

Objects are not properly copied when they are passed indirectly by value to a method #1451

Open
@josetr

Description

@josetr

The native method receives the same object, not a copy and the destructor ends up being called twice on the same object. The windows 32 bit target does copy the object by value *obj if its small enough but with a similar outcome since it doesn't call the copy constructor so it may end up trying to free an internal buffer twice.

public void TestObjectPassByValue1451()
{
    using (var s = new Issue1451())
    {
        s.A = 500;
        Assert.IsTrue(CSharp.CSharp.TestObjectPassByValue(s));
        Assert.That(s.A, Is.EqualTo(500));
    }
}
struct DLL_API Issue1451 {
  int a;
  int* b;
  static bool success;

  Issue1451();
};

Issue1451::Issue1451()
{
    a = 0;
    b = nullptr;
}

bool TestObjectPassByValue(Issue1451 s)
{
    s.a = 99999;
    return true;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions