Odd Destructor Behavior

Matt Elkins via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Feb 7 14:42:42 PST 2016


On Sunday, 7 February 2016 at 22:35:57 UTC, anonymous wrote:
> On 07.02.2016 23:07, Márcio Martins wrote:
>> The destructor you are seeing is from the assignment:
>>
>> m_tileView = TileView(...);
>>
>> This creates a temporary TileView, copies it to m_tileView, 
>> and then
>> destroys it. I suppose you want to move it instead. You need 
>> to copy the
>> handles from the temporary into the destination, and then 
>> clear them out
>> from the temporary to prevent them from being released.
>
> I think you're mistaken here. The result of a struct literal is 
> usually moved implicitly.
>
> Code:
> ----
> import std.stdio;
>
> struct S
> {
>     ~this() {writeln("dtor");}
> }
>
> void main()
> {
>     auto s = S();
>     writeln("end of main");
> }
> ----
>
> Output:
> ----
> end of main
> dtor
> ----
>
> If there was a copy that's destroyed after the assignment, 
> there should be another "dtor" before "end of main".

Yeah...and I just stuck this into TileView:
@disable this();
@disable this(this);

and it compiled just fine. If it created a copy I assume the 
compiler would have choked on that.


More information about the Digitalmars-d-learn mailing list