[Issue 19430] wrong code for `this =`, corrupted memory issue

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Nov 24 14:24:05 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=19430

--- Comment #6 from Илья Ярошенко <ilyayaroshenko at 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

--


More information about the Digitalmars-d-bugs mailing list