Unintentional sharing?

evilrat evilrat666 at gmail.com
Thu Jun 6 19:09:42 UTC 2024


On Thursday, 6 June 2024 at 17:49:39 UTC, Andy Valencia wrote:
> I was using instance initialization which allocated a new 
> object.  My intention was this initialization would happen 
> per-instance, but all instances appear to share the same 
> sub-object?  That is, f1.b and f2.b appear to point to a single 
> object?  Obviously I moved the new into the initializer code, 
> but I hadn't appreciated how initial instance values were 
> calculated once.  Interestingly, this makes it similar to how 
> Python calculates default argument values for functions.
>
> class Bar {
>     int z = 3;
> }
>
> class Foo {
>     auto b = new Bar();
> }
>
> void
> main() {
>     import std.stdio : writeln;
>
>     auto f1 = new Foo(), f2 = new Foo();
>     f1.b.z = 0;
>     writeln(f2.b.z);
> }

What you are seeing here is indeed sharing reference.
It happens because type initializer sets fields after memory 
allocation but before constructor call, and so since it is using 
value known at compile time all instances will have share same 
reference.

https://dlang.org/spec/class.html#constructors


More information about the Digitalmars-d-learn mailing list