Bug in RefCounted?

Kenji Hara k.hara.pg at gmail.com
Tue Oct 29 21:37:37 PDT 2013


On Tuesday, 29 October 2013 at 11:46:53 UTC, Rene Zwanenburg 
wrote:
> On Monday, 28 October 2013 at 19:30:12 UTC, Maxim Fomin wrote:
>> Here is my attempt:
>>
>> import std.stdio;
>>
>> struct S
>> {
>>   int i;
>>   this(int i)   { writefln("ctor, %X", i); this.i = i; }
>>   this(this)  { writefln("postblit, %X, %X", &this, i); }
>>   ~this()     { writefln("dtor, %X, %X", &this, i); }
>> }
>>
>> auto foo()
>> {
>>   S s = S(1);
>>   return { s = S(2); } ;
>> }
>>
>> void main()
>> {
>>   foo()();
>> }
>>
>> ctor, 1
>> dtor, 7FFFF7ED8FF8, 1
>> ctor, 2
>> dtor, 7FFFFFFFDB30, 1
>>
>> Inside foo() function object 's' is destroyed twice: first 
>> time as a regular struct at the end of block scope, second 
>> time before assigning S(2).
>>
>> There are other tools: union bug, control flow tricks, 
>> __traits, __dtor but they are move obvious.
>
> That's pretty nasty :). But I suspect this is a bug and not by 
> design. __dtor and __traits are, IMHO, the proverbial escape 
> hatch D should provide, so I think that's OK. I take it that by 
> control flow trick you mean the try/catch example in your other 
> post?
>
> Anyway, thanks for pointing this out. Will probably save me 
> some debugging in the future.

The combination of closure variables + scoped destruction should 
be rejected, but currently it isn't. It's a compiler bug.

http://d.puremagic.com/issues/show_bug.cgi?id=11382

Kenji Hara


More information about the Digitalmars-d-learn mailing list