Bug in RefCounted?

Maxim Fomin maxim at maxim-fomin.ru
Mon Oct 28 12:30:10 PDT 2013


On Monday, 28 October 2013 at 16:53:11 UTC, monarch_dodra wrote:
> On Monday, 28 October 2013 at 10:07:15 UTC, Rene Zwanenburg 
> wrote:
>> Yeah, I'd like to know this as well. I do remember structs 
>> allocated on the heap don't have their destructors called at 
>> all due to the GC not being precise, I think.
>>
>> But one object allowed to be destructed multiple times? That 
>> sounds really bad.. If that's true a lot of my code is 
>> probably incorrect.
>
> Hum... I seem to remember having replied earlier, but I guess I
> forgot to hit send.
>
> In any case, I could be mistaken, but I simply know that under
> certain circumstances, it can happen.

OK

> I don't know if that's a
> bug though. I'll try to find the cases where it happens.
>

So do you *know* cases or suspect that they may exists? Or 
remember some bug issue?

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.



More information about the Digitalmars-d-learn mailing list