November 24, 2018 [Issue 19430] wrong code for `this =`, corrupted memory issue | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=19430 --- Comment #7 from Stanislav Blinov <stanislav.blinov@gmail.com> --- What's going on is (pseudo-code) this: c = S(S.init).opAssign(S.sum(1, 2)); The opAssign is implicitly generated. Two instances are constructed in-place, one is 'main.s', the other is the argument to opAssign, no copies are made, no postblits are called. But then the argument of opAssign is getting destructed, and decrements count to 0. So 0 is the correct output here. For it to be 1, you *need* an explicit opAssign: void opAssign(S rhs) { if (ptr && *ptr) atomicOp!"+="(*ptr, 1); } If it were a C++ std::shared_ptr kind of reference count (i.e. not a static counter), then you'd need this kind of opAssign: void opAssign(S rhs) { import std.algorithm.mutation : swap; swap(ptr, rhs.ptr); } -- |
November 24, 2018 [Issue 19430] wrong code for `this =`, corrupted memory issue | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=19430 --- Comment #8 from Stanislav Blinov <stanislav.blinov@gmail.com> --- Working example: https://run.dlang.io/is/dnxQNx -- |
November 24, 2018 [Issue 19430] wrong code for `this =`, corrupted memory issue | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=19430 Илья Ярошенко <ilyayaroshenko@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|REOPENED |RESOLVED Resolution|--- |LATER --- Comment #9 from Илья Ярошенко <ilyayaroshenko@gmail.com> --- Ok, requires more research -- |
November 24, 2018 [Issue 19430] wrong code for `this =`, corrupted memory issue | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=19430 --- Comment #10 from Stanislav Blinov <stanislav.blinov@gmail.com> --- To be fair, calling assignment on an instance that hasn't been yet constructed (i.e. constructor didn't return) isn't the best of ideas. I know Phobos is doing this in places, but it's something that should really, really be avoided. -- |
November 24, 2018 [Issue 19430] wrong code for `this =`, corrupted memory issue | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=19430 Илья Ярошенко <ilyayaroshenko@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Resolution|LATER |INVALID -- |
April 05, 2019 [Issue 19430] wrong code for `this =`, corrupted memory issue | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=19430 --- Comment #11 from Илья Ярошенко <ilyayaroshenko@gmail.com> --- I am starting to dig deeper since I have more code that uses RC with D. The first one issue is https://issues.dlang.org/show_bug.cgi?id=19774 The second one related to generic `=` operator, still needs to be reduced. -- |
Copyright © 1999-2021 by the D Language Foundation