Jump to page: 1 2
Thread overview
[Issue 19430] wrong code for `this =`, corrupted memory issue
Nov 24, 2018
Stanislav Blinov
Nov 24, 2018
Stanislav Blinov
Nov 24, 2018
Stanislav Blinov
Nov 24, 2018
Stanislav Blinov
Nov 24, 2018
Stanislav Blinov
Nov 24, 2018
Stanislav Blinov
November 24, 2018
https://issues.dlang.org/show_bug.cgi?id=19430

Илья Ярошенко <ilyayaroshenko@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
            Summary|wrong code for `this =`     |wrong code for `this =`,
                   |                            |corrupted memory issue

--
November 24, 2018
https://issues.dlang.org/show_bug.cgi?id=19430

Stanislav Blinov <stanislav.blinov@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |stanislav.blinov@gmail.com

--- Comment #1 from Stanislav Blinov <stanislav.blinov@gmail.com> ---
I don't see wrong code here.

`this = ` is `this.opAssign(rhs)`. That opAssign in this case is implicit, copy
is omitted.

Add

void opAssign(S rhs)
{
    i = rhs.i;
    puts("a");
}

the output will be

c
a
d
d

--
November 24, 2018
https://issues.dlang.org/show_bug.cgi?id=19430

--- Comment #2 from Илья Ярошенко <ilyayaroshenko@gmail.com> ---
The number of the constructor (including postblits) must match the number of destructors. Assume you have a reference counted member. Then the code in the issue will cause a corrupted memory error.

--
November 24, 2018
https://issues.dlang.org/show_bug.cgi?id=19430

--- Comment #3 from Stanislav Blinov <stanislav.blinov@gmail.com> ---
You forgot a `puts` in the two-argument constructor. There are exactly two instances being constructed. One by returning from `sum`, the other one is `c` in main. First "d" in output is from assignment, second is on exiting main. All seems quite legit, i.e. WAD.

--
November 24, 2018
https://issues.dlang.org/show_bug.cgi?id=19430

--- Comment #4 from Илья Ярошенко <ilyayaroshenko@gmail.com> ---
Inter(In reply to Stanislav Blinov from comment #3)
> You forgot a `puts` in the two-argument constructor. There are exactly two instances being constructed. One by returning from `sum`, the other one is `c` in main. First "d" in output is from assignment, second is on exiting main. All seems quite legit, i.e. WAD.

Ah, yes. In the same time, I am sure there is definitely an issue. Will try to find better code example

--
November 24, 2018
https://issues.dlang.org/show_bug.cgi?id=19430

Stanislav Blinov <stanislav.blinov@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #5 from Stanislav Blinov <stanislav.blinov@gmail.com> ---
I'll close this for now, please reopen as needed.

--
November 24, 2018
https://issues.dlang.org/show_bug.cgi?id=19430

--- Comment #6 from Илья Ярошенко <ilyayaroshenko@gmail.com> ---
(In reply to Stanislav Blinov from comment #5)
> I'll close this for now, please reopen as needed.

import core.atomic;
import core.stdc.stdio;
import core.stdc.stdlib;
struct S
{
    static shared int* ptr;

    this(int i)
    {
        ptr = cast(shared int*) malloc(4);
        *ptr = 1;
    }

    pragma(inline, false)
    this(this)
    {
        if (ptr && *ptr)
            atomicOp!"+="(*ptr, 1);
    }

    ~this()
    {
        if (ptr && *ptr)
            atomicOp!"-="(*ptr, 1);
    }

    this(int r, int e)
    {
        this = sum(r, e);
    }

    static S sum(int r, int e)
    {
        return S(r + e);
    }
}

void main()
{
    auto s = S(1, 2);

    import std.stdio;
    writeln(*s.ptr);
}

================
output: 0
expected: 1

--
November 24, 2018
https://issues.dlang.org/show_bug.cgi?id=19430

Илья Ярошенко <ilyayaroshenko@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |---

--
November 24, 2018
https://issues.dlang.org/show_bug.cgi?id=19430

Илья Ярошенко <ilyayaroshenko@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|---                         |FIXED

--
November 24, 2018
https://issues.dlang.org/show_bug.cgi?id=19430

Илья Ярошенко <ilyayaroshenko@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|FIXED                       |---

--
« First   ‹ Prev
1 2