CppSharp icon indicating copy to clipboard operation
CppSharp copied to clipboard

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

Open josetr opened this issue 5 years ago • 0 comments

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;
}

josetr avatar Oct 25 '20 18:10 josetr