Thread overview
Does static ctor/dtor behave differently in 2.067-b2?
Feb 27, 2015
amber
Re: Does static ctor/dtor of struct behave differently in 2.067-b2?
Feb 27, 2015
amber
Feb 28, 2015
ketmar
Mar 01, 2015
amber
February 27, 2015
Hi All,

I am just wondering if dmd 2067-b2 invokes static this() and static ~this() differently to 2066.1

I have a strange bug when I use DMD2.067-b2 and it looks a lot like the static ctor/dtor are run from a separate thread to main() and unittest{} blocks. When I run under 2066.1 the code works correcly and behaves as if everything runs in the same thread (which is what I am expecting).

It's difficult to be sure because "thisTid" under 2067 returns an actual ID but under 2066.1 it always returns "Tid(std.concurrency.MessageBox)"


The code below doesn't have the bug but it's what I'm using to try and figure out the difference between 2066.1 and 2067.

---
import std.concurrency;
import std.stdio;

struct S
{
    static int val = 1;
    static this(){
        writefln("Ctor:%s", thisTid);
        val = 2;
    }
    static ~this() {
        writefln("Dtor:%s", thisTid);
        val = 0;
    }
}

unittest {
    S s;
    writefln("UT -- s:%s tid:%s", S.val, thisTid);
}

void main() {
    S s;
    writefln("MAIN -- s:%s tid:%s", S.val, thisTid);
}

---

Thanks,
amber
February 27, 2015
On Friday, 27 February 2015 at 23:50:51 UTC, amber wrote:
> Hi All,
[snip]
> Thanks,
> amber

[edited subject]

Sorry I should add that I'm talking about static ctor/dtor of struct.

The bug I see with 2.067-b2 is this:

1. static this() {} called and static fields of struct are initialised
2. app runs, static fields are initialised.
3. static ~this() {} called and static fields of struct are NOT initialised.


In step 3 with 2.066.1 all the static fields of struct are still initialised, as expected, and my app shuts down cleanly.

Thanks,
amber
February 28, 2015
On Fri, 27 Feb 2015 23:58:16 +0000, amber wrote:

> On Friday, 27 February 2015 at 23:50:51 UTC, amber wrote:
>> Hi All,
> [snip]
>> Thanks, amber
> 
> [edited subject]
> 
> Sorry I should add that I'm talking about static ctor/dtor of struct.
> 
> The bug I see with 2.067-b2 is this:
> 
> 1. static this() {} called and static fields of struct are initialised
> 2. app runs, static fields are initialised.
> 3. static ~this() {} called and static fields of struct are NOT
> initialised.
> 
> 
> In step 3 with 2.066.1 all the static fields of struct are still initialised, as expected, and my app shuts down cleanly.

is your struct GC-allocated? and can you provide dustmited code or something we can play with?

March 01, 2015
On Saturday, 28 February 2015 at 03:26:17 UTC, ketmar wrote:
> On Fri, 27 Feb 2015 23:58:16 +0000, amber wrote:
>
>> On Friday, 27 February 2015 at 23:50:51 UTC, amber wrote:
>>> Hi All,
>> [snip]
>>> Thanks, amber
>> 
>> [edited subject]
>> 
>> Sorry I should add that I'm talking about static ctor/dtor of struct.
>> 
>> The bug I see with 2.067-b2 is this:
>> 
>> 1. static this() {} called and static fields of struct are initialised
>> 2. app runs, static fields are initialised.
>> 3. static ~this() {} called and static fields of struct are NOT
>> initialised.
>> 
>> 
>> In step 3 with 2.066.1 all the static fields of struct are still
>> initialised, as expected, and my app shuts down cleanly.
>
> is your struct GC-allocated? and can you provide dustmited code or
> something we can play with?

Hi ketmar, thanks for replying.

I think I have figured out what was happening.

DMD 2.067 spins up 5 threads when running the unittests and DMD 2.066.1 only uses one thread. This change exposed a bug in the static ctor which I've now fixed.

Thanks,
amber