[Issue 17448] Move semantics cause memory corruption and cryptic bugs
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon May 30 17:02:31 UTC 2022
https://issues.dlang.org/show_bug.cgi?id=17448
--- Comment #49 from Steven Schveighoffer <schveiguy at gmail.com> ---
On discord, we worked out a case where the compiler is moving a struct and not
calling any opPostMove or other features (copy constructor, postblit, etc):
```d
import std.stdio;
struct S{
S* ptr;
this(int dummy) {ptr = &this;}
this(this) {ptr = &this;}
void opPostMove(const ref S oldLocation) nothrow {
ptr = &this;
}
}
void foo(S s){
assert(&s == s.ptr); // fails
}
S bar(){
auto s=S(1);
auto t=S(1);
if(true){ return t; }
return s;
}
void main(){ foo(bar()); }
```
So there is still a need for the compiler to be aware of opPostMove.
In this example, the return from bar cannot be NRVO optimized, and so a copy is
made and the postblit called. Then in the call to foo, the return value of bar
is moved into the parameter for foo.
--
More information about the Digitalmars-d-bugs
mailing list